Rhino3dm.py draw line

Hi All,

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:


model = R.File3dm()

p1 = R.Point3d(0,0,0)
p2 = R.Point3d(10,10,0)
l1 = R.LineCurve(p1,p2)

model.Objects.AddCurve(l1)

model.Write(R"TEST\Test-01.3dm", 0)

So I guess the question becomes, what is the difference between ‘line’ and ‘linecurve’?

Thanks in advance.

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

Rhinocommon is a bit lower level API. Not sure if this helps, but here is about curves in rhino common: Rhino - Determining Curve Object Types

1 Like

Thanks, I did find those resources, but it seems that RhinoScriptSyntax can only be accessed from within the Rhino environment - is that right?

In my case, operating external to Rhino is not mandatory, but definitely preferred. So Rhino3dm seems to be the best option.

File3dmObjectMaterialTable.AddLine takes two points: File3dmObjectTable — rhino3dm 8.4.0 documentation

So your script could be:

import rhino3dm as R

model = R.File3dm()

p1 = R.Point3d(0,0,0)
p2 = R.Point3d(10,10,0)

model.Objects.AddLine(p1, p2)

model.Write(R"TEST\Test-01.3dm", 0)

Thanks for the reply. I had tried this as well, but I got the same TypeError as with using the line as an argument for the AddLine method.

I’ll try your code again when I get the chance.

Ok, let me know. I just tried it and it works over here.

Hi @Gerrard_Hickson,

You might have a look at our Rhino.Inside Python samples.

– Dale

It worked for me this morning… not sure why it didn’t work earlier - Thanks anyway.

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).

Yes.

– Dale

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.

So, enjoy :wink: :

Thanks for the explanation - I assumed this was an ‘exists in memory, not in file’ problem, but I couldn’t find anything in the RhinoCommonAPI documentation (https://developer.rhino3d.com/api/rhinocommon/rhino.geometry.line) to indicate what I needed to do.

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.

If you have a helpful reference, please share it.

Is this what we’re talking about here - Object Tables?

https://developer.rhino3d.com/api/rhinocommon/rhino.docobjects.tables.objecttable#methods

So these methods are what I need to add objects to the document? Feels like I’m getting somewhere.

Yes, you are making progress.

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?

So many options.

Hi Scott,

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.

Regards

if you just want a 2d drawing why not create dxf file instead of 3dm file?

There is a python module for editing dxf files much like rhino3d does with 3dm files.

Some time ago I recall posting an example about dxf with python here on the forums, look for it.

here it is:

So is this a browser based web application, or a desktop console application?

A Desktop console application without user interaction with Rhino would be a Rhino.Inside app.

Rhino3DM by itself will not work as the geometric operations like Boolean of regions requires a Rhino license.

Or if the application is hosted in a Windows app in console app, then these samples might help: rhino-developer-samples/rhino.inside at 7 · mcneel/rhino-developer-samples · GitHub

Rhino.Inside.Python would allow the a Python Process to host Rhino: Rhino inside Python

Without using the Rhino interface, then there is no display and that would need to be supplied by the host application.

Does it seem like one of these might help?

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.