gsteven 发表于 2009-9-21 00:41:00

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

gsteven 发表于 2009-9-21 01:23:00

<p>找到方法了!</p><p>使用WblockCloneObjects</p><p>注意红色的部分,不过不知道能不能复制块参照,并且把块参照的引用块定义也一起复制过去,com里的copyobjects方法可是可以做到这一点的,我试验过。<br/>&nbsp;</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/>&nbsp;</pre><pre class="codeLine"></pre><pre class="codeLine">public static void CopyObjectsBetweenDatabases()</pre><pre class="codeLine">{</pre><pre class="codeLine">&nbsp;&nbsp;ObjectIdCollection acObjIdColl = new ObjectIdCollection();</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;// Get the current document and database</pre><pre class="codeLine">&nbsp;&nbsp;Document acDoc = Application.DocumentManager.MdiActiveDocument;</pre><pre class="codeLine">&nbsp;&nbsp;Database acCurDb = acDoc.Database;</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;// Lock the current document</pre><pre class="codeLine">&nbsp;&nbsp;using (DocumentLock acLckDocCur = acDoc.LockDocument())</pre><pre class="codeLine">&nbsp;&nbsp;{</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Start a transaction</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Open the Block table record for read</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BlockTable acBlkTbl;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpenMode.ForRead) as BlockTable;</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Open the Block table record Model space for write</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BlockTableRecord acBlkTblRec;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acBlkTblRec = acTrans.GetObject(acBlkTbl,</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpenMode.ForWrite) as BlockTableRecord;</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Create a circle that is at (0,0,0) with a radius of 5</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Circle acCirc1 = new Circle();</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acCirc1.SetDatabaseDefaults();</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acCirc1.Center = new Point3d(0, 0, 0);</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acCirc1.Radius = 5;</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Add the new object to the block table record and the transaction</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acBlkTblRec.AppendEntity(acCirc1);</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acTrans.AddNewlyCreatedDBObject(acCirc1, true);</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Create a circle that is at (0,0,0) with a radius of 7</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Circle acCirc2 = new Circle();</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acCirc2.SetDatabaseDefaults();</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acCirc2.Center = new Point3d(0, 0, 0);</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acCirc2.Radius = 7;</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Add the new object to the block table record and the transaction</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acBlkTblRec.AppendEntity(acCirc2);</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acTrans.AddNewlyCreatedDBObject(acCirc2, true);</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Add all the objects to copy to the new document</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ff3300">acObjIdColl = new ObjectIdCollection();</font></pre><pre class="codeLine"><font color="#ff3300">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acObjIdColl.Add(acCirc1.ObjectId);</font></pre><pre class="codeLine"><font color="#ff3300">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acObjIdColl.Add(acCirc2.ObjectId);</font></pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Save the new objects to the database</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acTrans.Commit();</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Unlock the document</pre><pre class="codeLine">&nbsp;&nbsp;}</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;// Change the file and path to match a drawing template on your workstation</pre><pre class="codeLine">&nbsp;&nbsp;string sLocalRoot = Application.GetSystemVariable("LOCALROOTPREFIX") as string;</pre><pre class="codeLine">&nbsp;&nbsp;string sTemplatePath = sLocalRoot + "Template\\acad.dwt";</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;// Create a new drawing to copy the objects to</pre><pre class="codeLine">&nbsp;&nbsp;DocumentCollection acDocMgr = Application.DocumentManager;</pre><pre class="codeLine">&nbsp;&nbsp;Document acNewDoc = acDocMgr.Add(sTemplatePath);</pre><pre class="codeLine">&nbsp;&nbsp;Database acDbNewDoc = acNewDoc.Database;</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;// Lock the new document</pre><pre class="codeLine">&nbsp;&nbsp;using (DocumentLock acLckDoc = acNewDoc.LockDocument())</pre><pre class="codeLine">&nbsp;&nbsp;{</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Start a transaction in the new database</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using (Transaction acTrans = acDbNewDoc.TransactionManager.StartTransaction())</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Open the Block table for read</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BlockTable acBlkTblNewDoc;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId,</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpenMode.ForRead) as BlockTable;</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Open the Block table record Model space for read</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BlockTableRecord acBlkTblRecNewDoc;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#f70909">acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc,</font></pre><pre class="codeLine"><font color="#f70909"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpenMode.ForRead) as BlockTableRecord;</font></pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Clone the objects to the new database</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#f70909">&nbsp;IdMapping acIdMap = new IdMapping();</font></pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ff0000">&nbsp;acCurDb.WblockCloneObjects(acObjIdColl, acBlkTblRecNewDoc.ObjectId, acIdMap,</font></pre><pre class="codeLine"><font color="#ff0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DuplicateRecordCloning.Ignore, false);</font></pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Save the copied objects to the database</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acTrans.Commit();</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Unlock the document</pre><pre class="codeLine">&nbsp;&nbsp;}</pre><pre class="codeLine"><br/>&nbsp;</pre><pre class="codeLine">&nbsp;&nbsp;// Set the new document current</pre><pre class="codeLine">&nbsp;&nbsp;acDocMgr.MdiActiveDocument = acNewDoc;</pre><pre class="codeLine">}</pre>

雪山飞狐_lzh 发表于 2009-9-21 09:28:00

<p>还有深度Clone</p><p>db.DeepCloneObjects(...)</p>

gsteven 发表于 2009-9-21 19:27:00

<p>深度clone有什么不一样吗?</p>

雪山飞狐_lzh 发表于 2009-9-21 19:43:00

<p>深层拷贝-复制数据</p><p>浅层拷贝-只复制引用</p><p>具体的要在使用的过程中去理解它</p>
页: [1]
查看完整版本: .NET有COM的CopyObjects吗?找到方法了!