1. In the cad2012 environment:
ObjectARX development kit must be referenced
Promptpointresult PPR = ed. getpoint ("Select Insert point :");
Point3d Pt = PPR. value; // The insert point is obtained here.
Utility. writetoeditor (Pt. tostring ());
Blockpath = "B _sample.dwg ";
Using (Database blkdb = new database (false, true ))
{
// Read drawing
Blkdb. readdwgfile (blockpath, system. Io. fileshare. Read, true, null );
Blkdb. closeinput (true );
Using (documentlock doclock = Doc. lockdocument () // This is required for multiple documents first; otherwise, an error is returned.
{
Using (transaction T = Doc. transactionmanager. starttransaction ())
{
String name = "AA"; // AA is a string that does not have the same name as any block in the blockpath file.
// Insert it as a new block
Objectid idbtr = Doc. database. insert (AA, blkdb, false );
// Create a ref to the block
Blocktable bt = (blocktable) T. GetObject (Doc. database. blocktableid, openmode. forread );
Blocktablerecord BTR = (blocktablerecord) T. GetObject (BT [blocktablerecord. modelspace], openmode. forwrite );
Using (blockreference BREF = new blockreference (PT, idbtr) // PT is a point3d coordinate, which is inserted into the current DWG file.
{
BTR. appendentity (BREF );
T. addnewlycreateddbobject (BREF, true );
}
T. Commit (); that's it. Thank you.
}
}
}
2. Remove from the CAD environment
Tdwgnet development kit must be referenced
Using (new services ())
{
Using (Database DB = new database (True, true ))
{
Using (transaction Ts = dB. transactionmanager. starttransaction ())
{
Using (blocktable bt = ts. GetObject (db. blocktableid, openmode. forwrite) as blocktable)
{
Blocktablerecord btr1 = new blocktablerecord ();
Database ODB = new database (false, false );
ODB. readdwgfile ("AAA. DWG", fileopenmode. openforreadandallshare, true, null );
ODB. closeinput (true );
Objectid objid = dB. insert ("AA", ODB, false); // insert it to the current DWG file.
Blocktablerecord BTR = new blocktablerecord (); // insert another block
BTR. Name = "000 ";
Circle C = new circle (New point3d (0, 0, 0), vector3d. zaxis, 100 );
BTR. appendentity (C );
TS. addnewlycreateddbobject (C, true );
Bt. Add (BTR );
TS. addnewlycreateddbobject (BTR, true );
}
TS. Commit ();
}
// Using (transaction Ts = dB. transactionmanager. starttransaction ())
//{
// Using (blocktable bt = (blocktable) Ts. GetObject (db. blocktableid, openmode. forread ))
//{
// Blocktablerecord BTR = ts. GetObject (BT [blocktablerecord. modelspace], openmode. forwrite) as blocktablerecord;
// Blockreference BR = new blockreference (New point3d (0, 0, 0), BT ["AA"]); // The block is inserted in the current DWG.
// BTR. appendentity (BR );
// Ts. addnewlycreateddbobject (BR, true );
//}
// Ts. Commit ();
//}
DB. saveas (path + "\ test. DWG", dwgversion. Current); // save the file
}
}
}