clone a block reference and create new block reference at same position (.net)

clone a block reference and create new block reference at same position (.net)

Anonymous
Not applicable
954 Views
2 Replies
Message 1 of 3

clone a block reference and create new block reference at same position (.net)

Anonymous
Not applicable

Through .net api is it possible to clone a block reference and create a new block reference from existing block reference.

 

Suppose i have 20 chairs as a single block reference type,

As editing  the color of individual chair is not possible ..I am planning to create a new block reference type for all the 20 chairs.. While cloning block reference type i would specify the color to ByLayer ..and move the objects to a different layer ..so that color of the chair would be the layers color 

 

Let's say i have 20 chairs and 10 to be in red and 10 to be in green..

i would create object of cloned block-reference type exactly coinciding with the position of the old Object of the old block reference type ...so that it would super impose the previous block reference objects and turn off the previous layer

0 Likes
955 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant

Hi,

 

I'm not sure to understand what you're trying to do, but it seems to me you do not need to clone anything.

Just edit the 'chair' block definition (BlockTableRecord instance) to put all the entities it contain on Layer "0" and cole "ByLayer" (or "ByBlock") and all the inserted block references (BlockReference instances) will reflect these changes.

This is the way AutoCAD blocks work.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

_gile
Consultant
Consultant

 

        private void SetByLayer(string blockName)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (!bt.Has("blockName"))
                {
                    ed.WriteMessage($"\nBlock '{blockName}' non found.");
                    return;
                }
                var btr = (BlockTableRecord)tr.GetObject(bt[blockName], OpenMode.ForRead);
                foreach (ObjectId id in btr)
                {
                    var ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                    ent.Layer = "0";
                    ent.ColorIndex = 256; // 0 for ByBlock
                }
                tr.Commit();
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes