In this post we will talk about inserting blocks using MultiCAD.NET API – one of the most frequently asked questions on our forum.
Let us assume there is a drawing that has block definitions for inserting water supply elements, for example, the blocks that represent valves of different types.
Each block definition contains two attributes for providing some extra information about elements.
- NAME – the element’s name (for example, “Ball”),
- LABEL – the element’s label (for example, “PC585-70”).
To represent block inserts in the drawing, MiltiCAD.NET provides the McBlockRef
class. All you need to do is to create an insert object and assign the name of the block to the BlockName
field.
1 2 3 4 5 |
McBlockRef refBlk = new McBlockRef(); refBlk.BlockName = "block_01"; refBlk.DbEntity.AddToCurrentDocument(); |
In MultiCAD.NET, block attributes are stored as common object properties. Therefore in order to insert a block with desired attributes values; it takes only getting an access to them using DbEntity.ObjectProperties
:
1 2 3 4 |
refBlk.DbEntity.ObjectProperties["NAME"] = “Ball”; refBlk.DbEntity.ObjectProperties["LABEL"] = “PC585-70”; |
The McDocument
class has a method that displays a dialog with a list of all existing block definitions for choosing a block to insert.
The command below inserts the selected block with given attributes values to the drawing. To input values, the command line is used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[CommandMethod("smpl_insertBlock", CommandFlags.NoCheck | CommandFlags.Redraw)] static public void smpl_insertBlock() { // Gets the active document McDocument doc = McDocumentsManager.GetActiveDoc(); if (doc == null) return; String selBlock = String.Empty; McObjectId currId; // Calls the block selection dialog if (doc.ShowSelectBlockForm(ref selBlock, out currId, McContext.MainWindow().Handle)) { McBlockRef refBlk = new McBlockRef(); refBlk.BlockName = selBlock; refBlk.DbEntity.AddToCurrentDocument(); // user input InputJig jig = new InputJig(); refBlk.DbEntity.ObjectProperties["NAME"] = jig.GetText("Please enter a tap type:"); refBlk.DbEntity.ObjectProperties["LABEL"] = jig.GetText("Please enter a tap label:"); } else { MessageBox.Show("No block selected"); } } |