.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Creating Nested Block

4 REPLIES 4
Reply
Message 1 of 5
sanjaymann
811 Views, 4 Replies

Creating Nested Block

Hi,

 

Can Anyone help me with a sample code for creating nested blocks.

 

Thanks & Regards

 

4 REPLIES 4
Message 2 of 5

Hi

 

This sample creates a nested block that contains a circle, then a parent block that contains a block reference of the nested block:

 

[CommandMethod("NestedBlock")]
public void NestedBlock()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    using (Transaction Tx = db.TransactionManager.StartTransaction())
    {
        BlockTable bt = Tx.GetObject(db.BlockTableId, OpenMode.ForWrite) 
            as BlockTable;

        BlockTableRecord btr = new BlockTableRecord();

        btr.Name = "ParentBlock";

        bt.Add(btr);

        Tx.AddNewlyCreatedDBObject(btr, true);


        BlockTableRecord btrNested = new BlockTableRecord();

        btrNested.Name = "NestedBlock";

        bt.Add(btrNested);

        Tx.AddNewlyCreatedDBObject(btrNested, true);

        Circle circle = new Circle(Point3d.Origin, Vector3d.ZAxis, 5.0);

        AddToBlock(circle, btrNested.ObjectId);

        BlockReference bref = new BlockReference(
            Point3d.Origin, 
            btrNested.ObjectId);

        AddToBlock(bref, btr.ObjectId);

        Tx.Commit();
    }
}

private ObjectId AddToBlock(Entity entity, ObjectId btrId)
{
    using (Transaction tr = btrId.Database.TransactionManager.StartTransaction())
    {
        try
        {
            BlockTableRecord btr = tr.GetObject(
                btrId,
                OpenMode.ForWrite)
                    as BlockTableRecord;

            ObjectId id = btr.AppendEntity(entity);

            tr.AddNewlyCreatedDBObject(entity, true);

            tr.Commit();

            return id;
        }
        catch (Autodesk.AutoCAD.Runtime.Exception ex)
        {
            ErrorStatus es = ex.ErrorStatus;

            tr.Abort();
            return ObjectId.Null;
        }
        catch (System.Exception ex)
        {
            string error = ex.Message;

            tr.Abort();
            return ObjectId.Null;
        }
    }
}

Regards,

Philippe.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Message 3 of 5

Hi,

 

Thanks for your time but when I executed the code no circle is visible in the drawing but when I click block editor the circle is displayed in both blocks. Please see attached image.

Message 4 of 5

The circle is not visible because the final block is not inserted in the drawing. If you want to see it in model space, you would need either to use INSERT command or insert it programmatically. The circle is displayed in both block because NestedBlock is nested in the ParentBlock, so this is the expected behavior.

 

If you are a beginer with the AutoCAD API, I suggest you take a look first at the following resources:

 

http://www.youtube.com/watch?v=WYj0J-DrCSA

 

http://usa.autodesk.com/adsk/servlet/index?id=18162650&siteID=123112

 

http://docs.autodesk.com/ACD/2013/ENU/index.html?url=files/GUID-4E1AAFA9-740E-4097-800C-CAED09CDFF12...

 

Regards,

Philippe.



Philippe Leefsma
Developer Technical Services
Autodesk Developer Network

Message 5 of 5

That was great. The code that you gave answers what I asked for.

 

I was creating nested blocks so that when I take this file  in Navisworks I could see a heirarchal tree structure like:

GreateGrandfather

                            Grandfather

                                             Father

                                                     Child

                                                           Circle

 

But When I take this file in Navisworks I get the structure as:

 

GreateGrandfather

                           GreateGrandfather

                                                 Grandfather

                                                            Grandfather       

                                                                       Father

                                                                             Father

                                                                                  Child

                                                                                        Child

                                                                                              Circle

Please see the attached image[File in Navisworks]

 

Also When I try to run the code with some existing entity in the drawing and call AddToBlock with this existing entity as parameter in place of circlr its throws error eAlreadyindatabase at line :

tr.AddNewlyCreatedDBObject(entity, True)

 

And Yes you are right I have been in to programming from last 10 years but with AutoCAD API I have been working on and off for last 2 years.

 

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost