# How to create a new Block Definition?

**URL:** <https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265>\
**Category:** Civil 3D\
**Created:** [November 11, 2021, 11:45am UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265 "2021-11-11T11:45:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![deividsteffens](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@deividsteffens](https://forum.dynamobim.com/u/deividsteffens)\
**Post date:** [November 11, 2021, 11:45am UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/1 "2021-11-11T11:45:25Z")

</div>

Hi, I’m trying to create a new block definition.  
At the end of my dynamo script I have several blockreferences and 3D polylines, I would like to create a block definition with all those objects, insert then as a block reference.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/3/73a78a2445bd0e98fe30d9639ba9dd7d196a7c97.png)

I searched on the foruns, custom packages and didn’t find anything.  
I was trying to follow this link [Creating an AutoCAD block using .NET - Through the Interface](https://www.keanw.com/2010/01/creating-an-autocad-block-using-net.html) , but I know little of python and none of [VB.net](http://VB.net).

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [November 11, 2021, 3:26pm UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/2 "2021-11-11T15:26:38Z")

</div>

Hi @deividsteffens,

You could try these nodes from the Civil 3D Toolkit.

 ![Blocks](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/6/b6e35e8356a8975481502faf4e7f728ab463fcf9.png)

---

<div class="post-metadata">

**Author:** ![engenharia04NEFVB](https://avatars.discourse-cdn.com/v4/letter/e/ed8c4c/32.png) [@engenharia04NEFVB](https://forum.dynamobim.com/u/engenharia04NEFVB)\
**Post date:** [November 11, 2021, 5:31pm UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/3 "2021-11-11T17:31:49Z")

</div>

That doesn’t work and I can’t use the first one, I’m working with cad objects, not dynamo geometry.  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/b/1b6e0dd6000712ac626db6045ce7193a12b7b538.png)

I’m Deivid Steffens, wrong login for this post.

---

<div class="post-metadata">

**Author:** ![Paolo\_Emilio\_Serra1](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/paolo_emilio_serra1/32/12902_2.png) [@Paolo\_Emilio\_Serra1](https://forum.dynamobim.com/u/Paolo_Emilio_Serra1)\
**Post date:** [November 12, 2021, 11:18am UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/4 "2021-11-12T11:18:02Z")

</div>

read the log file in the temp folder

---

<div class="post-metadata">

**Author:** ![deividsteffens](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@deividsteffens](https://forum.dynamobim.com/u/deividsteffens)\
**Post date:** [November 12, 2021, 11:32am UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/5 "2021-11-12T11:32:25Z")

</div>

Hi Paolo, that error doesn’t trigger the log.

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/f/dfcf295ddec750e534c68ee300f822265ac21e67.png)

I was worred that even if this worked I was not able to choose the name of the block definition, but I can use parameter renaming.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/3/63b810ba00891df91eafec0db2729ce02117f39b.png)

---

<div class="post-metadata">

**Author:** ![engenharia04NEFVB](https://avatars.discourse-cdn.com/v4/letter/e/ed8c4c/32.png) [@engenharia04NEFVB](https://forum.dynamobim.com/u/engenharia04NEFVB)\
**Post date:** [November 12, 2021, 7:48pm UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/6 "2021-11-12T19:48:42Z")

</div>

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/0/900f2fef896c02ce5bf300b0d3ddee168edeaaec.png)

I got something going, need to know how to get ObjectId of my objects.

```auto
# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

def CreateBlock():
  #ids = IN[0].GetObjectIds()
  objs = IN[0]
  ids = []
  blockId = ObjectId.Null
  with adoc.LockDocument():
    with adoc.Database as db:
      with db.TransactionManager.StartTransaction() as tr:
        for obj in objs:
          #ids.append(tr.GetObject(obj.Id, OpenMode.ForRead))
          ids.append(obj.GetObjectId())

        bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
        bt.UpgradeOpen()
        record = BlockTableRecord()
        record.Name = "Teste001"
        bt.Add(record)
        tr.AddNewlyCreatedDBObject(record, true)
        
        blockId = bt['Teste001']
        tr.Commit()
CreateBlock()

OUT = [ids,blockid]

```

---

<div class="post-metadata">

**Author:** ![engenharia04NEFVB](https://avatars.discourse-cdn.com/v4/letter/e/ed8c4c/32.png) [@engenharia04NEFVB](https://forum.dynamobim.com/u/engenharia04NEFVB)\
**Post date:** [November 12, 2021, 8:21pm UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/7 "2021-11-12T20:21:00Z")

</div>

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/0/20a70e5d8f1e375b234854e24228a352acbac65b.png)  
Ok, I was able to Create the Block Definition, now working on appending the objects.

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [November 12, 2021, 8:46pm UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/8 "2021-11-12T20:46:39Z")

</div>

A few thoughts:

- I’m assuming eventually you’ll add the block name as an input for the function. In that case, it would be good to check if a block with that name already exists before trying to create it. The Block Table class inherits from the [Symbol Table class](https://help.autodesk.com/view/OARX/2022/ENU/?guid=OARX-ManagedRefGuide-__OVERLOADED_Has_Autodesk_AutoCAD_DatabaseServices_SymbolTable), which has a _Has_ method to check this.
- If you want to copy the existing objects (which are already contained in the model space block table record) into the new block table record, you’ll need to use the _[Database.DeepCloneObjects](https://help.autodesk.com/view/OARX/2022/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_DatabaseServices_Database_DeepCloneObjects_ObjectIdCollection_ObjectId_IdMapping __MarshalAsUnmanagedType_U1__ bool)_ method.

---

<div class="post-metadata">

**Author:** ![deividsteffens](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@deividsteffens](https://forum.dynamobim.com/u/deividsteffens)\
**Post date:** [November 12, 2021, 9:40pm UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/9 "2021-11-12T21:40:51Z")

</div>

Hi mz, I will not give an input for the name, no worries about that.

I was looking for that code to clone the objects, I’m stuck on exactly that. When I find the full solution I post.

---

<div class="post-metadata">

**Author:** ![deividsteffens](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@deividsteffens](https://forum.dynamobim.com/u/deividsteffens)\
**Post date:** [November 13, 2021, 12:53am UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/10 "2021-11-13T00:53:52Z")

</div>

Finally was able to reach the solution.

```auto
# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

def CreateBlock(bName, objs):
  ids = []
  blockId = ObjectId.Null
  
  with adoc.LockDocument():
    with adoc.Database as db:
      with db.TransactionManager.StartTransaction() as tr:

        bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
        bt.UpgradeOpen()
        btr = BlockTableRecord()
        btr.Name = bName
        bt.Add(btr)
        tr.AddNewlyCreatedDBObject(btr, True)       

        for obj in objs:
          id = obj.InternalObjectId
          ids.append(id)
          entity = tr.GetObject(id, OpenMode.ForRead)
          cloneEntity = entity.Clone()
          btr.AppendEntity(cloneEntity)
          tr.AddNewlyCreatedDBObject(cloneEntity, True)   
        tr.Commit()
        

OUT = CreateBlock('Teste004',IN[0])

```

---

<div class="post-metadata">

**Author:** ![deividsteffens](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@deividsteffens](https://forum.dynamobim.com/u/deividsteffens)\
**Post date:** [November 13, 2021, 1:32am UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/11 "2021-11-13T01:32:51Z")

</div>

Now I just want to know, how do I return back to dynamo the block reference as a Dynamo Block Reference?

The return of my function now is an “Autodesk.AutoCAD.DatabaseServices.BlockReference”

```auto
# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

def CreateBlock(bName, objs):
  ids = []
  blockId = ObjectId.Null
  
  with adoc.LockDocument():
    with adoc.Database as db:
      with db.TransactionManager.StartTransaction() as tr:

        bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
        model = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
        bt.UpgradeOpen()
        btr = BlockTableRecord()
        btr.Name = bName
        bt.Add(btr)
        tr.AddNewlyCreatedDBObject(btr, True)
        blockId = bt[bName]
        
        for obj in objs:
          id = obj.InternalObjectId
          ids.append(id)
          entity = tr.GetObject(id, OpenMode.ForRead)
          cloneEntity = entity.Clone()
          entity.Erase()
          btr.AppendEntity(cloneEntity)
          tr.AddNewlyCreatedDBObject(cloneEntity, True)   
        
        br = BlockReference(Point3d.Origin, blockId)
        model.AppendEntity(br)
        tr.AddNewlyCreatedDBObject(br, True)
        tr.Commit()
        return br

OUT = CreateBlock(IN[0],IN[1])

```

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [November 13, 2021, 3:41am UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/12 "2021-11-13T03:41:50Z")

</div>

You’ll have to add a reference to _Autodesk.AutoCAD.DynamoNodes_ and then use _Document.Current.BlockByName_ with the name of the block.

Nice work, Deivid! These would be some good nodes to add to Camber. Are you interested in contributing?

---

<div class="post-metadata">

**Author:** ![deividsteffens](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@deividsteffens](https://forum.dynamobim.com/u/deividsteffens)\
**Post date:** [November 13, 2021, 4:30am UTC](https://forum.dynamobim.com/t/how-to-create-a-new-block-definition/70265/13 "2021-11-13T04:30:31Z")

</div>

Sure, I would like to.
