Plug-in Autocad C# How to check a block object is a object of class defined by user

Plug-in Autocad C# How to check a block object is a object of class defined by user

KinAkira
Advocate Advocate
1,041 Views
3 Replies
Message 1 of 4

Plug-in Autocad C# How to check a block object is a object of class defined by user

KinAkira
Advocate
Advocate

Hi,

I have some codes to test block objects as below code.

With:

Command "CircleCall" to create circle blocks (from class "BlockCircle")

Command "CircleTest" to test block object which created by "CircleCall" is from class "BlockCircle"

My problem is command "CircleTest" not work as wishes.

Can anyone help me!

Many thanks for your help.

 

        [CommandMethod("CircleCall")]
        public void cmd_CircleCall()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;            
            var ed = doc.Editor;

            var ppo = new PromptPointOptions("\nPlease select point");
            var poi = ed.GetPoint(ppo).Value;

            var blockCircle = new BlockCircle();
            blockCircle.BlCircle(poi, 4);
        }

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

            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                PromptEntityOptions peo = new PromptEntityOptions("\nPlease select block object");
                peo.SetRejectMessage("Not a Block");
                peo.AddAllowedClass(typeof(BlockReference), true);
                PromptEntityResult per = ed.GetEntity(peo);

                if (per.Status != PromptStatus.OK)
                {
                    return;
                }

                BlockReference bref = trx.GetObject(per.ObjectId, OpenMode.ForRead) as BlockReference;
                BlockTableRecord btr = (BlockTableRecord)trx.GetObject(bref.BlockTableRecord, OpenMode.ForRead);
                               

                if (bref is BlockCircle)
                    ed.WriteMessage("\nBlock object is Blockcircle");
                else
                    ed.WriteMessage("\nBlock object is not Blockcircle");

                trx.Commit();
            }
        }

public class BlockCircle
        {
            public string Name;
            public Point3d Basepoint;
            public void BlCircle(Point3d p,double radius)
            {
                var db = HostApplicationServices.WorkingDatabase;
                this.Basepoint = p;

                using (var tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bltb = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                    var modelspace = (BlockTableRecord)tr.GetObject(bltb[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                    var maxIndex = 0;

                    foreach(ObjectId id in modelspace)
                    {
                        if (id.ObjectClass.DxfName == "INSERT")
                        {
                            var blockref = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                            
                            if (blockref.Name.StartsWith ("Circleblock."))
                            {                                
                                int num = blockref.Name.IndexOf(".");
                                var curIndex = int.Parse(blockref.Name.Substring(num+1));
                                if (curIndex > maxIndex)
                                    maxIndex = curIndex;                                
                            }
                        }
                    }

                    var bltbrc = new BlockTableRecord();
                    bltbrc.Name = "Circleblock."+(maxIndex+1).ToString();
                    this.Name = bltbrc.Name;

                    if (!bltb.Has(bltbrc.Name))
                    {
                        bltbrc.Origin = new Point3d(0, 0, 0);

                        using (Circle dt = new Circle())
                        {
                            dt.Center = new Point3d(0, 0, 0);
                            dt.Radius = radius;

                            bltbrc.AppendEntity(dt);
                            tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
                            bltb.Add(bltbrc);
                            tr.AddNewlyCreatedDBObject(bltbrc, true);
                        }
                    }                    

                    MyPlugin k = new MyPlugin();
                    k.InsertBlockwithRotation(p,bltbrc.Name,0);

                    tr.Commit();
                }
            }
        }

public void InsertBlockwithRotation(Point3d insPt, string blockName, double beta){...}

 

0 Likes
Accepted solutions (1)
1,042 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

bref is BlockCircle

will always return false because the type of bref is BlockReference and there's no class or type relationship between BlockReference and BlockCircle.
Only you plugin knows what is the BlockCircle type.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 4

KinAkira
Advocate
Advocate

Hi @_gile ,

Thanks for your help,

Do you have anyway to resolve my problem ? Like "class line", when we have a entity and check type of it is line, we can access to its properties (ex: line.startpoint, line.endpoint,..). But my object class have to like block type (or group)

0 Likes
Message 4 of 4

_gile
Consultant
Consultant

I do not really understand what you want to achieve but you can have a constructor of the BlockCircle class which takes a block reference as argument or a a static method which also takes  a block reference as argument and retruns a BlockCircle instance.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub