I. Demand
Sometimes because of the large span and large amount of data, the data will be stored in several DWG files, such as a railway design drawings exist in several or even dozens of DWG files. If you want to merge into a DWG file, manual merging is too cumbersome, so you can use code merge (there is no point in merging DWG directly with different degrees, please note). This article uses the Teigha Library for encoding. second, directly on the code
<summary>///dwg file converted to DXF file, multiple DWG files merged into a DXF or DWG file///</summary>///<p Aram Name= "Pdwgfiles" ></param>///<param name= "Poutputfile" >dxf or dwg save paths </param>/// <returns></returns> public static bool Mergedwgfiles (list<string> pdwgfiles,string poutputfile, Dwgversion pdwgversion = dwgversion.ac1024) {if (mrservices = = null) {MR
Services = new services (); try {//requirement: Merge multiple DWG files into a dxf file database Destdb = new Database (FAL
SE, false); Open a DWG destdb.readdwgfile (pdwgfiles[0], Fileopenmode.openforreadandallshare, False, String.
Empty);
for (int i = 1; i < Pdwgfiles.count i++) {string pdwgfile = Pdwgfiles[i]; Objectidcollection pobjidscoll = new ObjectidcollectiOn ();
Database srcdb = new Database (false, false); Srcdb.readdwgfile (Pdwgfile, Fileopenmode.openforreadandallshare, False, String.
Empty);
TransactionManager srctmgr = Srcdb.transactionmanager; using (Transaction Ptransac = Srctmgr.starttransaction ()) {blocktable Srcbtab
Le = Srctmgr.getobject (Srcdb.blocktableid, openmode.forread) as blocktable;
ObjectId Srcmodelspaceid = Srcbtable[blocktablerecord.modelspace];
Blocktablerecord Pbtrecord = Srctmgr.getobject (Srcmodelspaceid, openmode.forread) as Blocktablerecord;
IEnumerator piterator = Pbtrecord.getenumerator (); while (Piterator.movenext ()) {ObjectId ObjID = (ObjectId) piterator.cu
rrent;
Pobjidscoll.add (ObjID); Entity pentity = Ptmgr.getobject (ObjID, openmode.forread) as Entity;
} TransactionManager desttmgr = Destdb.transactionmanager; using (Transaction Desttransac = Desttmgr.starttransaction ()) {block
Table destbtable = Desttmgr.getobject (Destdb.blocktableid, openmode.forread) as blocktable;
ObjectId Destmodelspaceid = Destbtable[blocktablerecord.modelspace];
idmapping Pidmap = new idmapping (); Block depth cloning. The first parameter is the collection of entity IDs in Source DWG, the second parameter is the ID of the destination DWG file model space table, and the third parameter is the mapping relationship between IDs (is the output value) srcdb.wblockcloneobjects (POBJIDSC
Oll, Destmodelspaceid, Pidmap, Duplicaterecordcloning.replace, false);
Desttransac.commit ();
} ptransac.commit (); }}//for End//Save as DXF file, Save13 on behalf of DXF file, SAVE12 representative to save as DWG file, AC1024 is cad2010 FileInfo pfinfo = new FileInfo
(Poutputfile); if (pFInfo.Extension.ToLower () = ". dxf") {Destdb.saveas (Poutputfile, Savetype.save13
, Pdwgversion, False, 16); else if (pFInfo.Extension.ToLower () = ". dwg") {Destdb.saveas (POUTP
Utfile, Savetype.save12, Pdwgversion, False, 16);
}else {MessageBox.Show ("conversion output file format not supported");
return false; } catch (Teigha.Runtime.Exception ex) {MessageBox.Show ("DWG conversion DXF failed:" + ex.)
message);
return false;
return true; }
Wblockcloneobjects is the key to implementation