How to copy a contents of dwg file to another dwg file on APS
I'm trying to work on something but it keeps throws exceptions on APS
but it works fine on AutoCAD desktop version
let me explain the main idea I have 3 files for example
Site Layout.dwg
villa_1.dwg
villa_2.dwg
open Site Layout.dwg file copy the contents of it to villa_1.dwg and villa_2.dwg
this is a sample code for simplicity
CopyDWGPlugin.cs
public class CopyDWGPlugin
{
[CommandMethod("COPYDWGDATA")]
public void CopyDwgData()
{
System.Console.WriteLine("PLUGIN LOADED SUCCESSFULLY");
string workingDir = Directory.GetCurrentDirectory();
string sourcePath = Path.Combine(workingDir, "input.dwg");
string targetPath = Path.Combine(workingDir, "output.dwg");
if (!File.Exists(sourcePath))
throw new Exception("Source DWG not found: " + sourcePath);
if (!File.Exists(targetPath))
throw new Exception("Target DWG not found: " + targetPath);
using (Database sourceDb = new Database(false, true))
{
sourceDb.ReadDwgFile(sourcePath, FileShare.Read, true, "");
using (Database targetDb = new Database(false, true))
{
targetDb.ReadDwgFile(targetPath, FileShare.ReadWrite, true, "");
ObjectIdCollection ids = new ObjectIdCollection();
using (Transaction tr = sourceDb.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(sourceDb.BlockTableId, OpenMode.ForRead);
BlockTableRecord ms =
(BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (ObjectId id in ms)
ids.Add(id);
IdMapping mapping = new IdMapping();
sourceDb.WblockCloneObjects(
ids,
targetDb.CurrentSpaceId,
mapping,
DuplicateRecordCloning.Replace,
false
);
tr.Commit();
}
targetDb.SaveAs(targetPath, DwgVersion.Current);
}
}
}
}
PackageContents.xml