I’m new to Rhino, and I’d like to create a Python app that opens Rhino, draws some geometry then saves and closes the file. Rhino3dm.py seems to be a good choice for this case.
My first problem is drawing a simple line. If I understand the basic methodology correctly, I need to create the geometry in memory, then add it to the document. Initially, I tried to draw a ‘line’, but that failed - eventually, I figured that I can use ‘linecurve’ instead which does work.
The mode.Objects.AddLine(l1) line throws a TypeError (‘incompatible function arguments’). But the type of l1 is rhino3dm.Line which is a valid input for AddLine according to the documents.
import rhino3dm as R
model = R.File3dm()
p1 = R.Point3d(0,0,0)
p2 = R.Point3d(10,10,0)
l1 = R.Line(p1,p2)
model.Objects.AddLine(l1)
model.Write(R"TEST\Test-01.3dm", 0)
I eventually figured that the following does work:
There is a simplified script syntax that can be used with Python, and not a bad place to start. Here are some methods to add geo directly. Rhino - Rhino.Python Guides
There seems to be a never ending series of options for controlling Rhino with Python, half the challenge is figure out which path to take.
This seems to suggest that Rhino3dm is mostly for fileIO operations, and Rhino.Inside is for more complex operation in Rhino… which implies that Rhino.Inside can do everything Rhino3dm can do plus more - is that fair enough to say?
The focus of my app is generating 3dm files, but there will be some boolean operations in that so I’ll consider Rhino.Inside in case I need more advanced features later.
Here’s what I have so far:
The goal is to create a new file, draw a line, save and close.
Its saving the file, and it’s generating a line (at least it’s printing a valid length value) but when I open the document, the line is not there.
What’s happening here?
import rhinoinside
rhinoinside.load()
import System
import Rhino
doc = Rhino.RhinoDoc.CreateHeadless('')
# for now, you need to explicitly use floating point
# numbers in Point3d constructor
pt1 = Rhino.Geometry.Point3d(0.0,0.0,0.0)
pt2 = Rhino.Geometry.Point3d(10.0,10.0,0.0)
line = Rhino.Geometry.Line(pt1, pt2)
print(line.Length)
FN = R"TEST\Test-RhinoInside.3dm"
opt = Rhino.FileIO.FileWriteOptions()
doc.WriteFile(FN, opt)
Also worth nothing, Rhino3dm seems to perform much faster - about 5x in this simple case. Having said that, Rhino.Inside is still fast enough for my applicaiton (so far).
from the looks of it you’re missing the very crucial part.
This line here:
Adding whatever you create to the file.
Before this line of code everything you do, every object you create you do so in the RAM, in the temporary memory of the PC.
Consider the Rhino’s 3dm file as a portable database, and the model.Objects or the ObjectsTable is a table in that database that contains all the objects (visible, hidden, locked) along with all their properties, not just the geometry.
You need to add the objects you create to that table, otherwise they remain in the memory and when the py script is finished the memory is wiped clean.
As for that, you’ve no idea how right you are.
I’ve been struggling before when you could only use IronPython2 but most of the examples were in VB.net or C#.
Now, however beautiful for the inclusion of rhino3dm and even CPython incorporated inside Rhino8…Now is a complete mess. My IronPython scripts don’t work right away. So I am learning a lot again. And add to that the fact that I don’t work with Rhino on a daily basis the confusion is complete.
Even when I search directly for “Rhino model.Objects.AddLine” I can’t find anything that clearly explains the method (in the first few links), or what other similar methods (AddCurve, AddCircle?) I need to be aware of.
Perhaps explaining what you are trying to solve specifically would help us answer some of these questions.
The many rhino apis you are looking at are different entry points into Rhino tools based on the situation. Is the environment based on a rhinodoc available? Or is there another doc type? Or not a doc at all?
The goal is to take a text file with a list of parameters, and use those parameters to create a simple 2D drawing that will be converted to a PDF for use in a production environment.
I need access to basic geometry like lines, arcs, circles, but also external blocks. I haven’t decided yet, but I think I would also like the ability to perform boolean operation on regions (I believe PolyCurve in Rhino).
I want to be able to perform all operations without needing the user to open or interact with Rhino. It should be a case of click ‘go’ on a userform (HTML potentially) and a PDF is created and returned.
In any case, understanding which documentation applies to which method/entry point would be very helpful too.
This is not completely out of the question - however, after creation the drawings will be reviewed, updated (if required) by a user. For that reason, I’d prefer to keep them in a CAD native format.
Desktop console - the interface will be on an intranet page, with a very simple file select and “GO” button. No interaction with the document itself.
A (much) later stage might include a file preview, but I’ll cross that bridge if/when I come to it.
Rhino.Inside is probably the direction I’ll head. I think Rhino3dm could be workable (I can handle boolean operations in Python if required), but having something with more features is most likely the best path to take.
I have seen the Rhino inside Python link before - is there a good API reference for this somewhere? I’m finding that the documentation seems to be (more or less) shared across platforms on the https://developer.rhino3d.com/api/rhinocommon/ site.