.NET有COM的CopyObjects吗?找到方法了!
本帖最后由 作者 于 2009-9-21 1:43:08 编辑 <br /><br /> <p>com有object.CopyObjects(Objects[, Owner][, IDPairs]) ,object是<a href="mk:@MSITStore:E:\New%20Study\CAD\acadautovba.chm::/idh_database_object.htm">Database</a>, <a href="mk:@MSITStore:E:\New%20Study\CAD\acadautovba.chm::/idh_document_object.htm">Document</a>用于将一个document的图形复制到另外一个document当中,而且在 CopyObjects 操作期间,被 Objects 参数中的源对象拥有或引用的对象也将被复制。</p><p>不知道.net有没有相同功能的函数?查看了对象浏览器的document和database对象,都没有类似的方法</p> <p>找到方法了!</p><p>使用WblockCloneObjects</p><p>注意红色的部分,不过不知道能不能复制块参照,并且把块参照的引用块定义也一起复制过去,com里的copyobjects方法可是可以做到这一点的,我试验过。<br/> </p><pre class="codeLine">using Autodesk.AutoCAD.Runtime;</pre><pre class="codeLine">using Autodesk.AutoCAD.ApplicationServices;</pre><pre class="codeLine">using Autodesk.AutoCAD.DatabaseServices;</pre><pre class="codeLine">using Autodesk.AutoCAD.Geometry;</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"></pre><pre class="codeLine">public static void CopyObjectsBetweenDatabases()</pre><pre class="codeLine">{</pre><pre class="codeLine"> ObjectIdCollection acObjIdColl = new ObjectIdCollection();</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Get the current document and database</pre><pre class="codeLine"> Document acDoc = Application.DocumentManager.MdiActiveDocument;</pre><pre class="codeLine"> Database acCurDb = acDoc.Database;</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Lock the current document</pre><pre class="codeLine"> using (DocumentLock acLckDocCur = acDoc.LockDocument())</pre><pre class="codeLine"> {</pre><pre class="codeLine"> // Start a transaction</pre><pre class="codeLine"> using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())</pre><pre class="codeLine"> {</pre><pre class="codeLine"> // Open the Block table record for read</pre><pre class="codeLine"> BlockTable acBlkTbl;</pre><pre class="codeLine"> acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,</pre><pre class="codeLine"> OpenMode.ForRead) as BlockTable;</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Open the Block table record Model space for write</pre><pre class="codeLine"> BlockTableRecord acBlkTblRec;</pre><pre class="codeLine"> acBlkTblRec = acTrans.GetObject(acBlkTbl,</pre><pre class="codeLine"> OpenMode.ForWrite) as BlockTableRecord;</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Create a circle that is at (0,0,0) with a radius of 5</pre><pre class="codeLine"> Circle acCirc1 = new Circle();</pre><pre class="codeLine"> acCirc1.SetDatabaseDefaults();</pre><pre class="codeLine"> acCirc1.Center = new Point3d(0, 0, 0);</pre><pre class="codeLine"> acCirc1.Radius = 5;</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Add the new object to the block table record and the transaction</pre><pre class="codeLine"> acBlkTblRec.AppendEntity(acCirc1);</pre><pre class="codeLine"> acTrans.AddNewlyCreatedDBObject(acCirc1, true);</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Create a circle that is at (0,0,0) with a radius of 7</pre><pre class="codeLine"> Circle acCirc2 = new Circle();</pre><pre class="codeLine"> acCirc2.SetDatabaseDefaults();</pre><pre class="codeLine"> acCirc2.Center = new Point3d(0, 0, 0);</pre><pre class="codeLine"> acCirc2.Radius = 7;</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Add the new object to the block table record and the transaction</pre><pre class="codeLine"> acBlkTblRec.AppendEntity(acCirc2);</pre><pre class="codeLine"> acTrans.AddNewlyCreatedDBObject(acCirc2, true);</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Add all the objects to copy to the new document</pre><pre class="codeLine"> <font color="#ff3300">acObjIdColl = new ObjectIdCollection();</font></pre><pre class="codeLine"><font color="#ff3300"> acObjIdColl.Add(acCirc1.ObjectId);</font></pre><pre class="codeLine"><font color="#ff3300"> acObjIdColl.Add(acCirc2.ObjectId);</font></pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Save the new objects to the database</pre><pre class="codeLine"> acTrans.Commit();</pre><pre class="codeLine"> }</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Unlock the document</pre><pre class="codeLine"> }</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Change the file and path to match a drawing template on your workstation</pre><pre class="codeLine"> string sLocalRoot = Application.GetSystemVariable("LOCALROOTPREFIX") as string;</pre><pre class="codeLine"> string sTemplatePath = sLocalRoot + "Template\\acad.dwt";</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Create a new drawing to copy the objects to</pre><pre class="codeLine"> DocumentCollection acDocMgr = Application.DocumentManager;</pre><pre class="codeLine"> Document acNewDoc = acDocMgr.Add(sTemplatePath);</pre><pre class="codeLine"> Database acDbNewDoc = acNewDoc.Database;</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Lock the new document</pre><pre class="codeLine"> using (DocumentLock acLckDoc = acNewDoc.LockDocument())</pre><pre class="codeLine"> {</pre><pre class="codeLine"> // Start a transaction in the new database</pre><pre class="codeLine"> using (Transaction acTrans = acDbNewDoc.TransactionManager.StartTransaction())</pre><pre class="codeLine"> {</pre><pre class="codeLine"> // Open the Block table for read</pre><pre class="codeLine"> BlockTable acBlkTblNewDoc;</pre><pre class="codeLine"> acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId,</pre><pre class="codeLine"> OpenMode.ForRead) as BlockTable;</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Open the Block table record Model space for read</pre><pre class="codeLine"> BlockTableRecord acBlkTblRecNewDoc;</pre><pre class="codeLine"> <font color="#f70909">acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc,</font></pre><pre class="codeLine"><font color="#f70909"> OpenMode.ForRead) as BlockTableRecord;</font></pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Clone the objects to the new database</pre><pre class="codeLine"> <font color="#f70909"> IdMapping acIdMap = new IdMapping();</font></pre><pre class="codeLine"> <font color="#ff0000"> acCurDb.WblockCloneObjects(acObjIdColl, acBlkTblRecNewDoc.ObjectId, acIdMap,</font></pre><pre class="codeLine"><font color="#ff0000"> DuplicateRecordCloning.Ignore, false);</font></pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Save the copied objects to the database</pre><pre class="codeLine"> acTrans.Commit();</pre><pre class="codeLine"> }</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Unlock the document</pre><pre class="codeLine"> }</pre><pre class="codeLine"><br/> </pre><pre class="codeLine"> // Set the new document current</pre><pre class="codeLine"> acDocMgr.MdiActiveDocument = acNewDoc;</pre><pre class="codeLine">}</pre> <p>还有深度Clone</p><p>db.DeepCloneObjects(...)</p> <p>深度clone有什么不一样吗?</p> <p>深层拷贝-复制数据</p><p>浅层拷贝-只复制引用</p><p>具体的要在使用的过程中去理解它</p>
页:
[1]