Python, what's wrong with you?

1 Like

lower case “if” ?

2 Likes

Dam… coming from Rhinoscript it might take awhile to get used to…

1 Like

Hi @Asterisk, also keep in mind that you must call the methods properly eg:

rs.ClearCommandHistory
rs.UnselectAllObjects

will just return reference to the function itself and does nothing without parenthesis. It should be like below instead:

rs.ClearCommandHistory()
rs.UnselectAllObjects()

_
c.

1 Like

Is this the same?

rs.Command("_CPlane", echo=False)
vs
rs.Command("_CPlane", False)

Is there more elegant way of orienting CPlane perp to curve in default position
than this?

image

In this case yes.

vb Rhinoscript and Python rhinoscriptsyntax do differ on how they approach supplying arguments to methods.

in vb, if you have several arguments, and you are not going to specify them all, you can just leave a space like this

Rhino.GetObjects("message",,,True)
Here, I skipped the object filter and the group arguments, but specified “preselect” to be True

In python, that is not allowed. However, you can skip arguments by discreetly (specifically) naming those that come after (otherwise known as “keyword arguments”):

rs.GetObjects("message", preselect=True)
Which does the same thing as the vb version above.

One other thing to know about this is that arguments still have to be in the correct order, and that you cannot use a non-specified argument in a position after one in which you have specified an argument name.

So

rs.GetObjects("message", preselect=True, select=False) is OK.

but

rs.GetObjects("message", preselect=True, False) is not.

In the case of your rs.Command() there are only two arguments to specify, the command string itself and whether you want to echo or not, so assuming you always specify a command string as the first argument, you can either use the keyword or not for the second.

HTH, --Mitch

1 Like

Thank God! is rs.DimScale() = Rhino.DimStyleScale?

Well, you could use native rhinoscriptsyntax methods - it also depends if you want to actually set the CPlane in the viewport, or if you just need a plane perpendicular to the curve at some point to do some other stuff which does not require user interaction. In the second case, I would calculate the plane without changing the CPlane in the viewport (less invasive).

You will need to know a little about how planes are handled perhaps. Here is a code snippet for setting the active viewport’s CPlane to the midpoint of the selected curve.

import rhinoscriptsyntax as rs

crv=rs.GetObject("Select a curve",4)
if crv:
    mp=rs.CurveMidPoint(crv)
    param=rs.CurveClosestPoint(crv,mp)
    frame=rs.CurvePerpFrame(crv,param)
    #frame will be a plane perpendicular to the curve at the midpoint
    #set the current viewport CPlane to the frame plane:
    rs.ViewCPlane(rs.CurrentView(),frame)
1 Like

^ Thanks that’s exactly what I needed. I need to print out all the rs methods and read what they do. CurvePerpFrame is what I was missing.

Not sure… DimStyleScale does not exist in python rhinoscriptsyntax, and DimScale does not exist in vb rhinoscript… nor does it seem to exist in V6 rhinoscriptsyntax, only V5… So not sure what to tell you here.

I’m still on R5 for a time being. So how do I get a current dim style scale in python? I use it to “scale” the text that script would add to the document.

Also, is rs.LastCreatedObjects()[0] = Rhino.LastCreatedObects(0)?

In V5, this doesn’t seem possible via python, the method is not exposed in RhinoCommon. :frowning_face: It is in V6.

Yes. Just beware that with either language, if no “last objects” have been created, the script will error out - because you are trying to get an item from an array/list that doesn’t exist. Better is to do this:

lco=rs.LastCreatedObjects()
if lco:
    obj=lco[0]
1 Like

Maybe using the scripted command _ScaleDimstyle to increase the size in V5 ?

_
c.

Hmm, thanks for pointing out that command Clement, don’t think I ever used it - it doesn’t exist anymore in V6 looks like.

Hi Mitch, i did not notice that it’s gone in V6.

Maybe if @Asterisk only requires to scale the Text, he could also use the TextHeight property of the dimension style. Eg like this in python, this makes the change on the current dimension style, tested in V5 only.

import scriptcontext

dim_style = scriptcontext.doc.DimStyles.CurrentDimensionStyle
dim_style.TextHeight = 2.0
dim_style.CommitChanges()

scriptcontext.doc.Views.Redraw()

it is the same as rs.DimStyleTextHeight function…

or use this to control the text size when adding it.
_
c.

1 Like

The problem with that is that it’s the same (0.1) for all our dim styles and only Model space scale number change.

So the workaround is kinda ugly, but acceptable - pull a leader in a dim style I need (current), explode it and clean up leftover curves. Then I can read the text height, 'cause it’ll be 0.1 multiplied by w/e current dim style scale is. I see even R5 python doesn’t have masking and mask margin exposed, so I’m forced to use -Text command to get masked text created.

Any idea on why text object text isn’t being “processed” or how to “process” it so it shows what it’s supposed to show without manually double clicking the text and escaping out of it?

import rhinoscriptsyntax as rs

OX = rs.ViewCPlane()
CL = rs.CurrentLayer()

if not rs.IsLayer(“DIMs::SysDepths”):
rs.AddLayer(“DIMs::SysDepths”, (250, 0, 0))
if (CL.find(“SysDepths”) == -1):
rs.CurrentLayer(“DIMs::SysDepths”)

rs.ClearCommandHistory()
print “Draw a system depth line FROM PANEL FACE back to substrate.”

while True:
rs.Command("_Line", False)
SDL = rs.LastCreatedObjects()

if (SDL):
    SDL=SDL[0]
else:
    break

#rs.EnableRedraw(False)

rs.ObjectPrintWidth(SDL,-1)
rs.ObjectColor(SDL,(105,0,0))
rs.ReverseCurve(SDL)
rs.UnselectAllObjects()
rs.SelectObject(SDL)
rs.Command("_CPlane C _Enter",False)
rs.Command("_Dimcurvelength 1,2,0 2,2,0 _Enter",False)
rs.UnselectAllObjects()
rs.SelectObject (rs.FirstObject())
rs.Command("_Explode",False)
tmpLdr = rs.LastCreatedObjects()
for x in range(0,len(tmpLdr)):
    if rs.IsText(tmpLdr[x]):
        txHt = rs.TextObjectHeight(tmpLdr[x])
rs.DeleteObjects(tmpLdr)
rs.Command("-Text 2,2,0.5 F " + chr(34) + "Times New Roman" + chr(34) + " H " + str(txHt) + " Mask=Yes UseBackgroundColor=No MaskColor=255,255,255 MaskMargin=.1 xYz",False)
lbl = rs.FirstObject()
rs.MoveObject(lbl, rs.CurveEndPoint(SDL)-rs.CurveStartPoint(SDL))
rs.TextObjectText(lbl,"%<curvelength(" + chr(34) + str(SDL) + chr(34) + ")>%")
rs.TextObjectStyle(lbl,1)
rs.AddObjectsToGroup((SDL,lbl),rs.AddGroup())
rs.ViewCPlane(None,OX)
#rs.EnableRedraw(True)

rs.ViewCPlane(None,OX)
rs.CurrentLayer(CL)

This works perfectly in any view other than true plan view (top down onto CPlane). Why? Why does it matter? I’ve not had this issue with Rhinoscript scripts.

It will fail if I disable Redraw and it will fail if I’m in a true top down view (all tested views are parallel projection if that matters).

So, I figured why the script fails with redraw off or in plan view where leader that’s getting exploded is not visible - rs.Command(“Explode”) on a leader does not create a text that normally happens when exploding leaders straight in Rhino…

And changing rs.Command("_Dimcurvelength 1,2,0 2,2,0 _Enter",False) to rs.AddLeader(([0,0,0],[1,0,0]),None,“xYz”) forces rs.Cmnd(“Explode”) to create a text… I guess python doesn’t like text fields…

That also fixed the issue with disabling redraw…

And changing this

rs.TextObjectText(lbl,"%<curvelength(" + chr(34) + str(SDL) + chr(34) + “)>%”)

to

tmp = rs.coercerhinoobject(lbl, True, True)
tmp.Geometry.TextFormula = "%<curvelength(" + chr(34) + str(SDL) + chr(34) + ")>%"
tmp.CommitChanges()

fixed the text field/formula issue. I just have no idea what the hell it’s doing 'cause there’s no doc on it that I could find in Rhino.

The documentation for rhinoscriptsyntax is online. Also partly visible through the rhino Python editor.

https://developer.rhino3d.com/api/RhinoScriptSyntax/

However by coercing a Rhino object you have crossed the boundary into RhinoCommon