Polyline in python

Hi,

how about:

my_rectangle = rc.Geomtry.Rectangle3D(Plane,Point3D,Point3D)
poly = my_rectangle.ToPolyline()

first create the my_rectangle as Rectangle3d object
then call function ToPolyline for my_rectangle

or in one line:

poly =  rc.Geomtry.Rectangle3D(Plane,Point3D,Point3D).ToPolyline()

does this make sense?

-Willem

I tried this:

import Rhino as rc
import rhinoscriptsyntax as rs

BlocHg=rc.Geometry.Point3d(0,0,0)
BlocBd=rc.Geometry.Point3d(200,200,0)
Pla=rs.ViewCPlane()
Bloc=rc.Geometry.Rectangle3d(Pla,BlocHg,BlocBd).ToPolyline()

it doesn’t work!

so i passed by Rhinoscriptsyntax:

rs.AddRectangle(rs.MovePlane(rs.WorldXYPlane(),box[3]), DimX, -DimY)

You successfully created the polyline with RhinoCommon, but it’s still virtual geometry, it has not been added to the document, so you don’t see it. That is what the scriptcontext module mentioned above can do. Try this:

import Rhino as rc
import rhinoscriptsyntax as rs
import scriptcontext as sc

BlocHg=rc.Geometry.Point3d(0,0,0)
BlocBd=rc.Geometry.Point3d(200,200,0)
Pla=rs.ViewCPlane()
Bloc=rc.Geometry.Rectangle3d(Pla,BlocHg,BlocBd).ToPolyline()
sc.doc.Objects.AddPolyline(Bloc)
sc.doc.Views.Redraw()

You can also do this:

import Rhino as rc
import rhinoscriptsyntax as rs
import scriptcontext as sc

BlocHg=rc.Geometry.Point3d(0,0,0)
BlocBd=rc.Geometry.Point3d(200,200,0)
Pla=rs.ViewCPlane()
Bloc=rc.Geometry.Rectangle3d(Pla,BlocHg,BlocBd)
sc.doc.Objects.AddRectangle(Bloc)
sc.doc.Views.Redraw()

Rhinoscriptsyntax methods generally add the newly created objects to the document and assign them a GUID, but RhinoCommon doesn’t.

1 Like

ok i made 2 python scripts yesterday, and i put them in button…
when i launch the first one it’s ok, the second, it’s ok too…
but if i launch again the first one, my display turns off and on again with a change, like when my pc dis the display driver …

can you try my script on your rhino, put them in a new button…

! _-RunPythonScript (
# coding: utf-8
import Rhino as rc
import rhinoscriptsyntax as rs

def GeneratePt(Diam,Delta):
    #definition du centre du polygone
    Center=rc.Geometry.Point3d(Delta/2+Diam/2,Delta/2+Diam/2,0)
    Pts=[]
    Pts.append(rc.Geometry.Point3d(0,Delta+Diam,0))
    Pts.append(rc.Geometry.Point3d(Delta/2+Diam/2,Delta+Diam,0))
    Pts.append(rc.Geometry.Point3d(Delta/2+Diam/2,Delta/2+Diam,0))
    for i in range(1,13):
    #create the transform
        xform=rs.XformRotation2(i*(360/12),rs.ViewCPlane().Normal,Center)
        #transform le dernier point realise
        Pts.append(rs.PointTransform(Pts[2],xform))
    Pts.append(rc.Geometry.Point3d(Delta/2+Diam/2,Delta+Diam,0))
    Pts.append(rc.Geometry.Point3d(Delta+Diam,Delta+Diam,0))
    return Pts

Diam=rs.GetInteger('Diamètre à découper',15)
Delta=rs.GetInteger('Décalage de la forme',6)

PtTraj=GeneratePt(Diam,Delta)
rs.AddPolyline(PtTraj)
)
! _-RunPythonScript (

# coding: utf-8
import Rhino as rc
import rhinoscriptsyntax as rs
import math


DimX=rs.GetInteger('dimension du bloc en X',200)
if DimX:DimY=rs.GetInteger('dimension du bloc en Y',200)
if DimY:Traj=rs.GetObject('Selectionne la trajectoire a repeter',rs.filter.curve)
if Traj:DeltaX=rs.GetInteger('decalage en X',0)
DeltaY=rs.GetInteger('decalage en Y',5)

#boite englobante Traj
if Traj:
    box = rs.BoundingBox(Traj)
    if box:
        Trajx=box[2][0]-box[0][0]+DeltaX
        Trajy=box[2][1]-box[0][1]+DeltaY
#Trace le Bloc
Bloc=rs.AddRectangle(rs.MovePlane(rs.WorldXYPlane(),box[3]), DimX, -DimY)
rs.ObjectColor( Bloc,(255,0,0))

#Nbre d'élement en X
Nbx=math.floor(DimX/Trajx)
#Nbre d'élement en y
Nby=math.floor(DimY/Trajy)
Nb=int(Nbx*Nby)
#récupère les points de la Traj
ListPts = rs.CurvePoints(Traj)
#création de la liste de point ptTraj
PtTraj=[]
#création de la ligne X
ListX=[]
for j in range(0,int(Nbx)):
    ListTemp=[]
    ajout_en_x=j*Trajx
    for Pt in ListPts:
        PtTemp=rc.Geometry.Point3d(Pt)
        PtTemp[0]+=ajout_en_x
        ListTemp.append(PtTemp)
    ListX+=ListTemp

I tried on another computer Windows seven… the same problem,
i tried with a Windows 10 computer, and it’s seams ok…
so i have to pass to Windows 10 i think!

hi @AndersDeleuran, do you know why the result is still shown as list of points? though when i check the type it’s already a polyline.

it display the ‘curve’/‘polyline’ after I added this:

pl_crv = pl.ToPolylineCurve()

not sure why it needs that step.

A polyline in RhinoCommon is just a list of points…

1 Like

Just to elaborate a bit, the Polyline class is a child of the Point3dList class. And when you output any enumerable (i.e. a tuple, list, dict, collections etc.) from GHPython, the component will automagically unpack this into a Grasshopper list. Which can sometimes be confusing (e.g. in the case of a dict).

3 Likes