How to make a block referecne with my position.

How to make a block referecne with my position.

MarkJamesRogolino
Advocate Advocate
195 Views
1 Reply
Message 1 of 2

How to make a block referecne with my position.

MarkJamesRogolino
Advocate
Advocate

Hello, everyone. Now I make a code to make a block referecne with customized position.

But if I use my position any block reference is not made. Could someone help me?

my code is as followings.

 

 

 

 

 

var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
int refcircount = 0;
refcircount++;
            PromptPointResult pPtRes;
            PromptPointOptions pPtOpts = new PromptPointOptions("");
            // Prompt for the start point
            pPtOpts.Message = "\nEnter the center point of the line: ";
            pPtRes = ed.GetPoint(pPtOpts);
            Point3d ptCenter = pPtRes.Value;

            if (pPtRes.Status == PromptStatus.OK)
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable acBlkTbl;
                    acBlkTbl = tr.GetObject(db.BlockTableId,
                                                 OpenMode.ForRead) as BlockTable;
                    BlockTableRecord acBlkTblRec = new BlockTableRecord();
                    acBlkTblRec.Name = "DirectionRef_PreVal "+ refcircount.ToString();
                    acBlkTbl.UpgradeOpen();
                    ObjectId btrId = acBlkTbl.Add(acBlkTblRec);
                    tr.AddNewlyCreatedDBObject(acBlkTblRec, true);

                    dirrefcount++;
                    DBObjectCollection ents = makefloorcircle(ptCenter);
                    foreach (Entity ent in ents)
                    {
                        acBlkTblRec.AppendEntity(ent);
                        tr.AddNewlyCreatedDBObject(ent, true);
                    }
                    DBObjectCollection ents1 = makeResiBUAcircle(ptCenter);
                    foreach (Entity ent1 in ents1)
                    {
                        acBlkTblRec.AppendEntity(ent1);
                        tr.AddNewlyCreatedDBObject(ent1, true);
                    }
                    BlockTableRecord ms = (BlockTableRecord)tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    BlockReference br =new BlockReference(ptCenter, btrId);
                    ms.AppendEntity(br);
                    tr.AddNewlyCreatedDBObject(br, true);
                    tr.Commit();
                }
            }

 

 

 

 

 

Here If I use BlockReference br =new BlockReference(Point3d.Origin, btrId); it works well. Thank you in advance.

0 Likes
196 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor

Firstly, it looks like your code defines multiple block definitions with the same entity elements (so they would look the same when inserted as BlockReferences) with different names. I am wondering why. You should simply create the block definition ONCE and insert block references at different locations as needed, just as user does it manually.

 

Secondly, you should build the block definition with Point3d.Origin as base point, not the user-picked point, as your code shows (the function makefloorcircle()/makeResiBUAcircle()). The user picked point is used as the block refernece insertion point.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes