Get Block Parent ObjectID from Child

DirtyDanLand
Enthusiast
Enthusiast

Get Block Parent ObjectID from Child

DirtyDanLand
Enthusiast
Enthusiast

I am struggling to find the parent Object ID of a child block.  I already have the ObjectID of the child object but I can't figure out how to get the parent objected without prompting the user to select it.  I was exploring GetContainer but cant get it to work without prompting the user for selection.

 

Example: my block "FUSE-BLOCK" (this is variable) has a child block "END-NODE" (this is constant).  I already have the ObjectID of the child block "END-NODE".

 

Any help is greatly appreciated, I am doing this in C#, AutoCAD 2015.

0 Likes
Reply
1,267 Views
5 Replies
Replies (5)

ActivistInvestor
Advisor
Advisor

If there can be any number of occurrences of a parent block reference, Each of which contains an occurrence of a given child element, then you have to identify one of parents. You can do that either by prompting the user to select one or using some other criteria to find it. 

 

Beyond that, I think you would need to further explain what your ultimate objective is and/or include some code. 

 

 


@DirtyDanLand wrote:

I am struggling to find the parent Object ID of a child block.  I already have the ObjectID of the child object but I can't figure out how to get the parent objected without prompting the user to select it.  I was exploring GetContainer but cant get it to work without prompting the user for selection.

 

Example: my block "FUSE-BLOCK" (this is variable) has a child block "END-NODE" (this is constant).  I already have the ObjectID of the child block "END-NODE".

 

Any help is greatly appreciated, I am doing this in C#, AutoCAD 2015.


 

0 Likes

DirtyDanLand
Enthusiast
Enthusiast

I'll try to explain better since my code is all over the place trying to figure this out...

 

I have a block named: "PARENTBLOCKA" (this will be a variable name) 

I have a block called: "PARENTBLOCKB(this will be a variable name) 

Both the "PARENTBLOCKA" and "TERMBLOCK" have an attribute called "TAG"

Within the "PARENTBLOCKA": there are multiple other blocks called "END-NODE" (END-NODE is always the same)

Within the "PARENTBLOCKB": there are multiple other blocks called "END-NODE" (END-NODE is always the same)

The "END-NODE" block contains two attributes.

 

The startpoint of a polyline starts at the insertion point of "END-NODE" within the "PARENTBLOCKA".  The endpoint of the polyline ends at the insertion point of "END-NODE" within the "PARENTBLOCKB".  I am finding the "END-NODE" block by looking at the startpoint and endpoint of the polyline for a block called "END-NODE".  I need to be able to get the "TAG" attribute from the "PARENTBLOCKA" and "PARENTBLOCKB" for the respective "END-NODE" without prompting the user to select the object.  I already have the ObjectID of the "END-NODE" block at both ends of the polyline and I know which one is at the start and end, just need to figure out how to get the parent block of the "END-NODE".

 

Hope that makes sense...

 

0 Likes

ActivistInvestor
Advisor
Advisor

I think you should read my previous response again because you are obviously not understanding what I said perhaps I didn't explain it well enough. You cannot identify a given instance of a parent given nothing more than the object ID of a child element. Because there can be any number of instances of the parent, each having the same child element. If you don't want to require the user to select the power and send you must come up with some other means of finding them.

0 Likes

kerry_w_brown
Advisor
Advisor

How are you determining the ObjectID of the Child Block "END-NODE" ??

 

 


// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.
Sometimes the question is more important than the answer.

class keyThumper<T> : Lazy<T>;      another  Swamper

0 Likes

DirtyDanLand
Enthusiast
Enthusiast

I got it to work by iterating through the blocks inserted into model space.  For our use, I don't need anything from paper space.  Each block in model space is checked for a nested block with the "END-NODE" name, if it has the "END-NODE" nested block, the position of the "END-NODE" is checked to see if it is the same as the end of the polyline.  If it matches, the ObjectID of the block is returned.

 

The code is a little rough right now and needs to be cleaned up, but it works the way I need it to.  Some of the code is modified code found elsewhere.

        public static ObjectId FindEndNodeParent(string sEndNodeName, Point3d pntEndNodeLocation)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction trans = doc.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord ms = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForRead);

                BlockTableRecordEnumerator it = ms.GetEnumerator();
                while (it.MoveNext())
                {
                    Entity ent = (Entity)trans.GetObject(it.Current, OpenMode.ForRead);
                    if (ent.GetType() == typeof(BlockReference))
                    {
                        BlockReference blkRef = (BlockReference)ent;
                        //MessageBox.Show(blkRef.Name);

                        if (IsNodeParent(blkRef, sEndNodeName, pntEndNodeLocation) == true)
                         {
                            return blkRef.ObjectId;
                        }

                        if (blkRef.IsDynamicBlock)
                        {
                            // Here you have a DynamicBlock reference.
                        }
                    }
                }
                it.Dispose();

                trans.Commit();
            }
            return ObjectId.Null;
        }

        private static Boolean IsNodeParent(BlockReference parentBlockRef, string sEndNodeName, Point3d pntEndNodeLocation)
        {
            var trans = parentBlockRef.Database.TransactionManager.TopTransaction;
            var btr = (BlockTableRecord)trans.GetObject(parentBlockRef.BlockTableRecord, OpenMode.ForRead);
            foreach (ObjectId id in btr)
            {
                if (id.ObjectClass.Name == "AcDbBlockReference")
                {
                    var nestedBlockRef = (BlockReference)trans.GetObject(id, OpenMode.ForRead);
                    if (nestedBlockRef.Name == sEndNodeName & nestedBlockRef.Position == pntEndNodeLocation)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return false;
        }
0 Likes