[Pin to top] C # insert another DWG graph block in CAD

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.