Python to create some entities, create a block and insert

As a newby in Python I have made a function to create a so called BaseLine (for use in Ship designs). I want to create a block directly from the new entities and insert it. I can’t find a example script to help me create a block from generated entities. The examples I found are all based on user selection first and that is not what I want. Can anybody show how to do it with a couple of different entities like a line,a circle and a text?

Thanks in advance, Erik

Hi,
as far as i understand block insert in rhino, following steps are involved.

  1. create a geometry
  2. add this geometry to the rhino document to get a object (ID)
  3. create a link to this object (GeometryBase)
  4. create a BlockInstanceDefenition from the link
  5. find this BlockInstanceDefenition in your rhino document
  6. add a BlockInstanceObject (its the geometry link object)
import Rhino


# create your geometry
act_doc = Rhino.RhinoDoc.ActiveDoc
point_A = Rhino.Geometry.Point3d(0,0,0)
point_B = Rhino.Geometry.Point3d(30,0,0)
geo_line = Rhino.Geometry.Line(point_A,point_B)



# add the geometry to the rhino doc to get a rhino object
obj_Id = Rhino.RhinoDoc.ActiveDoc.Objects.AddLine(geo_line)
geo_base = Rhino.DocObjects.ObjRef(obj_Id).Geometry()
Rhino.RhinoDoc.ActiveDoc.Objects.Delete(obj_Id,True)


# define attributes
attr = Rhino.DocObjects.ObjectAttributes()



# create a block (InstanceDefenition) from your rhino doc object 
Rhino.RhinoDoc.ActiveDoc.InstanceDefinitions.Add('Block_test','Nothing to descripe',point_A,geo_base,attr)



# insert block object geometry to the rhino doc
trans = Rhino.Geometry.Transform.Translation(0,0,0)
for i in Rhino.RhinoDoc.ActiveDoc.InstanceDefinitions:
    if i.Name == 'Block_test':
        Rhino.RhinoDoc.ActiveDoc.Objects.AddInstanceObject(i.Index,trans)

I have no idea if this is the right way to create and insert a block but hopefully it gives you a idea how to start.

Hi Flokart,

at first I want to apologize for not responding sooner, but I was to busy lately to try your solution. This is certainly working, thanks for that. It is only missing my main issue: how to build a list with entities to put in the block? My application creates a lot of lines and text and I not able build the list of entities. Could you extent your example with a method to make that list and use it in the block?
thx, Erik

You have to save your geometry’s GeometryBase to a list, ideally while still running your command. Then use this list as an argument for the InstanceDefinitions.Add method. In the above example, this happend in

 geo_base = Rhino.DocObjects.ObjRef(obj_Id).Geometry()

Hi rgr, thanks! i can understand that part, but how to create the list and add the objects?

usually, when you create your geometry(like list etc), you add them to a list of your own, just as you would do with any other object in python.

eg

geometries = []
pt1 = Rhino.Geometry.Point3d(0,0,0)
pt2 = Rhino.Geometry.Point3d(10,0,0)
pt3 = Rhino.Geometry.Point3d(10,10,0)

#you can not cast a point3d to geometrybase, so you have to add a point object instead and give the point3d as an argument
geometries.append(Rhino.Geometry.Point(pt1))
geometries.append(Rhino.Geometry.Point(pt2))
geometries.append(Rhino.Geometry.Point(pt3))

attribute = Rhino.DocObjects.ObjectAttributes()
index = Rhino.RhinoDoc.ActiveDoc.InstanceDefinitions.Add('block','description', pt1, geometries,attribute)

Rhino.RhinoDoc.ActiveDoc.Objects.AddInstanceObject(index, Rhino.Geometry.Transform.Identity)

Thanks rgr!