getting object type of entity inside a foreach loop looking through an ObjectIdCollection

getting object type of entity inside a foreach loop looking through an ObjectIdCollection

Yehoshua18
Enthusiast Enthusiast
888 Views
7 Replies
Message 1 of 8

getting object type of entity inside a foreach loop looking through an ObjectIdCollection

Yehoshua18
Enthusiast
Enthusiast

I'm running through a list of objects to place them in a block here. I want to pick out one of the entities from the bunch, check if it's an Mtext, and then if it is, I wan it to be sent back in the draw order.  I can't figure out how to address an individual entity's properties as it gets looped over. 

 

Thanks for the help!

                        blockTable.UpgradeOpen();
                        blockTable.Add(btr3);
                        trans3.AddNewlyCreatedDBObject(btr3, true);
                        ObjectId idSendBack;


                        ObjectIdCollection entityCopyCol = new ObjectIdCollection();
                        foreach (ObjectId objectId in ObjID)
                        {
                            Entity entity = (Entity)trans3.GetObject(objectId, OpenMode.ForRead);
                            Entity entityCopy = (Entity)entity.Clone();
                            btr3.AppendEntity(entityCopy);
                            trans3.AddNewlyCreatedDBObject(entityCopy, true);
                            entityCopyCol.Add(entityCopy.ObjectId);
                            if (entityCopy.GetType() is MText)
                            {
                                doc.Editor.WriteMessage("an entity was found ot type MText");
                                idSendBack = entityCopy.ObjectId;
                            }

                        }


                        drawOrder.MoveBelow(entityCopyCol, idSendBack);

 

0 Likes
Accepted solutions (1)
889 Views
7 Replies
Replies (7)
Message 2 of 8

Keith.Brown
Advisor
Advisor
Accepted solution

I always use this when querying object types with objectids

 

If (objectId.ObjectClass.DxfName == “MText”)

You can get the dxfname of any entity by using the LIST command.

Message 3 of 8

Yehoshua18
Enthusiast
Enthusiast

So I've managed so far, but now the program crashes when line "drawOrder.MoveBelow(entityCopyCol, idSendBack);" runs. Any advice? 

 

                    using (Transaction trans3 = db1.TransactionManager.StartTransaction())
                    {
                        BlockTable blockTable = (BlockTable)trans3.GetObject(db1.BlockTableId, OpenMode.ForRead);
                        BlockTableRecord btr3 = new BlockTableRecord();

                        DrawOrderTable drawOrder = trans3.GetObject(btr.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;
                        


                        btr3.Name = "UT_Check_1";
                        int i = 1;


                        while (blockTable.Has(btr3.Name))
                        {
                            i++;
                            btr3.Name = "UT_Check_" + i.ToString();
                        }

                        blockTable.UpgradeOpen();
                        blockTable.Add(btr3);
                        trans3.AddNewlyCreatedDBObject(btr3, true);
                        ObjectId idSendBack = ObjectId.Null;


                        ObjectIdCollection entityCopyCol = new ObjectIdCollection();
                        foreach (ObjectId objectId in ObjID)
                        {
                            Entity entity = (Entity)trans3.GetObject(objectId, OpenMode.ForRead);
                            Entity entityCopy = (Entity)entity.Clone();
                            btr3.AppendEntity(entityCopy);
                            trans3.AddNewlyCreatedDBObject(entityCopy, true);
                            entityCopyCol.Add(entityCopy.ObjectId);
                            if (entityCopy.GetType() == typeof(MText)) 
                            {
                                MText MtextSendBack = entityCopy as MText; 

                                if (MtextSendBack.Contents.Contains("Status"))
                                {

                                    doc.Editor.WriteMessage("an entity was found ot type MText");
                                    idSendBack = entityCopy.ObjectId;
                                    if (idSendBack != ObjectId.Null)
                                    {
                                        drawOrder.MoveBelow(entityCopyCol, idSendBack);
                                    }
                                }
                            }
                        }
                        

                        Point3d origin = new Point3d(0, 0, 0);

                        BlockReference blockRef = new BlockReference(origin, btr3.ObjectId);
                        BlockTableRecord modelSpace = (BlockTableRecord)trans3.GetObject(db1.CurrentSpaceId, OpenMode.ForWrite);
                                              
                        modelSpace.AppendEntity(blockRef);
                        trans3.AddNewlyCreatedDBObject(blockRef, true);

                        foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(typeof(UTOptions)))
                        {
                            AttributeDefinition attrDef = new AttributeDefinition();
                            attrDef.Position = new Point3d(0, 0, 0);
                            attrDef.Prompt = property.Name;
                            attrDef.Tag = property.Name;
                            attrDef.TextString = property.GetValue(utoptions)?.ToString() ?? "";
                            attrDef.Height = 2.5;
                            attrDef.Justify = AttachmentPoint.MiddleCenter;
                            attrDef.Visible = false;
                            //attrDef.Layer = utoptions.confltlyr;
                            AttributeReference attRef = new AttributeReference();
                            attRef.SetAttributeFromBlock(attrDef, blockRef.BlockTransform);
                            blockRef.AttributeCollection.AppendAttribute(attRef);
                            trans3.AddNewlyCreatedDBObject(attRef, true);
                        }

                        AttributeDefinition date = new AttributeDefinition();
                        date.Position = new Point3d(0, 0, 0);
                        date.Prompt = "Date Created";
                        date.Tag = "Date Created";
                        date.TextString = String.Format("{0}", DateTime.Now.ToString());
                        date.Height = 2.5;
                        date.Justify = AttachmentPoint.MiddleCenter;
                        date.Visible = false;
                        //date.Layer = utoptions.confltlyr;
                        AttributeReference dater = new AttributeReference();
                        dater.SetAttributeFromBlock(date, blockRef.BlockTransform);
                        blockRef.AttributeCollection.AppendAttribute(dater);
                        trans3.AddNewlyCreatedDBObject(dater, true);

                        trans3.Commit();
                    }

 

0 Likes
Message 4 of 8

Yehoshua18
Enthusiast
Enthusiast

Thanks that works well! I found another solution as I was going, but I really apricate your answer! Now I'm having an issue with changing the draw order.

0 Likes
Message 5 of 8

norman.yuan
Mentor
Mentor

The crash caused by calling "drawOrder.MoveXXX()" is rather obvious: the draworder object and the entity that you want to change its draw order DO NOT BELONG to the same BlockTableRecord: the former (variable "drawOrder") belongs to a BlockTableRecord "btr" (your code DOES not show what it is, but I assume it is the current space (ModelSpace, or PaperSpace block), while the latter ("isSendBack", i.e. "entitCopy") belongs to the newly created block definition. YOU SHOULD set the "drawOrder" variable to the DrawOrderTable of the newly created block definition!

 

Also, your later half of code, which is not related to the error of setting draw order, is also quite wrong: it seems to me that you do not know where AttributeDefinition should belong to and what is the relationship between AttributeDefinition and AttributeReference. AttributeDefinition should be added into block definition; while creating AttributeReference, it can either use an AttributeDefinition IN THE block definition as template (calling SetAttributeFromBlock()) as template, or it can be create without AttributeDefinition at all (but it may have some unexpected side effect, say, when using the Xpress tool command "ATTSYNC"). In your code, you should not create an AttributeDefinition and not add it into a BlockTableRecord.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 6 of 8

Yehoshua18
Enthusiast
Enthusiast

hmm sorry. I'm too new at this to really understand your comment. I haven't had any problems createing the block itself. It seems to function as I want it to. These block attributes are not meant to be user edited. They are more meant to be a record of the calculations and settings that went into creating the block in the first place. 

 

With regards to the drawOrder.MoveXXX() command, I moved it into what I bilevel is the correct space for it. The problem now is that idSendBack is null, even when the loop gets triggered. Even the object collection entityCopyCol, I created for the Cloned entities, is populated with 10 entries all of which are null. 

 

This doesn't make sense to me, as the original object ID collection is populated properly. 

 

Do the cloned objects keep the same object Id? That does not seem likely to me. 

 

Thanks!

 


                    // put all drawn objects into a block. 
                    Document doc1 = Application.DocumentManager.MdiActiveDocument;
                    Database db1 = doc1.Database;



                    using (Transaction trans3 = db1.TransactionManager.StartTransaction())
                    {
                        BlockTable blockTable = (BlockTable)trans3.GetObject(db1.BlockTableId, OpenMode.ForRead);
                        BlockTableRecord btr3 = new BlockTableRecord();

                        
                        


                        btr3.Name = "UT_Check_1";
                        int i = 1;


                        while (blockTable.Has(btr3.Name))
                        {
                            i++;
                            btr3.Name = "UT_Check_" + i.ToString();
                        }

                        blockTable.UpgradeOpen();
                        blockTable.Add(btr3);
                        trans3.AddNewlyCreatedDBObject(btr3, true);
                        ObjectId idSendBack = ObjectId.Null;


                        ObjectIdCollection entityCopyCol = new ObjectIdCollection();
                        foreach (ObjectId objectId in ObjID)
                        {
                            Entity entity = (Entity)trans3.GetObject(objectId, OpenMode.ForRead);
                            Entity entityCopy = (Entity)entity.Clone();
                            entityCopyCol.Add(entityCopy.ObjectId);
                            if (entityCopy.GetType() == typeof(MText))
                            {
                                MText MtextSendBack = entityCopy as MText;

                                if (MtextSendBack.Contents.Contains("Status"))
                                {

                                    doc.Editor.WriteMessage("\n You Entity Was Found!");
                                    idSendBack = entityCopy.ObjectId;


                                }
                            }


                            btr3.AppendEntity(entityCopy);
                            trans3.AddNewlyCreatedDBObject(entityCopy, true);
                        }



                        DrawOrderTable drawOrder = trans3.GetObject(btr3.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;

                        if (idSendBack != ObjectId.Null)
                        {
                            drawOrder.MoveAbove(entityCopyCol, idSendBack);
                        }





                        Point3d origin = new Point3d(0, 0, 0);

                        BlockReference blockRef = new BlockReference(origin, btr3.ObjectId);
                        BlockTableRecord modelSpace = (BlockTableRecord)trans3.GetObject(db1.CurrentSpaceId, OpenMode.ForWrite);
                                              
                        modelSpace.AppendEntity(blockRef);
                        trans3.AddNewlyCreatedDBObject(blockRef, true);

                        foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(typeof(UTOptions)))
                        {
                            AttributeDefinition attrDef = new AttributeDefinition();
                            attrDef.Position = new Point3d(0, 0, 0);
                            attrDef.Prompt = property.Name;
                            attrDef.Tag = property.Name;
                            attrDef.TextString = property.GetValue(utoptions)?.ToString() ?? "";
                            attrDef.Height = 2.5;
                            attrDef.Justify = AttachmentPoint.MiddleCenter;
                            attrDef.Visible = false;
                            //attrDef.Layer = utoptions.confltlyr;
                            AttributeReference attRef = new AttributeReference();
                            attRef.SetAttributeFromBlock(attrDef, blockRef.BlockTransform);
                            blockRef.AttributeCollection.AppendAttribute(attRef);
                            trans3.AddNewlyCreatedDBObject(attRef, true);
                        }

                        AttributeDefinition date = new AttributeDefinition();
                        date.Position = new Point3d(0, 0, 0);
                        date.Prompt = "Date Created";
                        date.Tag = "Date Created";
                        date.TextString = String.Format("{0}", DateTime.Now.ToString());
                        date.Height = 2.5;
                        date.Justify = AttachmentPoint.MiddleCenter;
                        date.Visible = false;
                        //date.Layer = utoptions.confltlyr;
                        AttributeReference dater = new AttributeReference();
                        dater.SetAttributeFromBlock(date, blockRef.BlockTransform);
                        blockRef.AttributeCollection.AppendAttribute(dater);
                        trans3.AddNewlyCreatedDBObject(dater, true);

                        trans3.Commit();

                        
                    }

 

0 Likes
Message 7 of 8

Yehoshua18
Enthusiast
Enthusiast

Interesting, 

the enitityCopy does not have a object id.... how could that be? 

Yehoshua18_0-1680596644425.png

 

 

0 Likes
Message 8 of 8

Yehoshua18
Enthusiast
Enthusiast
0 Likes