.NET

Reply
Distinguished Contributor
sanjaymann
Posts: 126
Registered: ‎05-22-2013
Message 1 of 5 (274 Views)

Creating Nested Block

274 Views, 4 Replies
09-18-2013 02:12 AM

Hi,

 

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

 

Thanks & Regards

 

ADN Support Specialist
philippe.leefsma
Posts: 658
Registered: ‎06-02-2009
Message 2 of 5 (241 Views)

Re: Creating Nested Block

09-24-2013 01:57 AM in reply to: sanjaymann

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

Distinguished Contributor
sanjaymann
Posts: 126
Registered: ‎05-22-2013
Message 3 of 5 (232 Views)

Re: Creating Nested Block

09-24-2013 03:03 AM in reply to: philippe.leefsma

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.

ADN Support Specialist
philippe.leefsma
Posts: 658
Registered: ‎06-02-2009
Message 4 of 5 (224 Views)

Re: Creating Nested Block

09-24-2013 04:29 AM in reply to: sanjaymann

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

Distinguished Contributor
sanjaymann
Posts: 126
Registered: ‎05-22-2013
Message 5 of 5 (189 Views)

Re: Creating Nested Block

09-24-2013 11:14 PM in reply to: philippe.leefsma

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.

 

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!