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
}
}
}