- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi there,
I am having problems completing the following instrucción for autoCAD:
Given some DWG and a Base Drawing i need to do the following:
create a new directory including copies of the Base drawing with the name of the other DWGs.
Copy the model space entities from the DWG to the new base DWG with the same name.
I found the command WblockCloneObject() quite interesting to develop the command but i am not able to make it work. From the code below, no error is generated but also no entities have been copied.
I need also to do some copy of the paper space (that is why there is a function called CopyingFromPaperSpace), but that would come later after i have solved this issue.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System.Windows;
namespace AYE_SG_ExportacionesCAD_STT
{
public class Commands
{
[CommandMethod("NewCommand")]
public static void NewCommand()
{
DocumentCollection docCol = Application.DocumentManager;
Document doc = docCol.MdiActiveDocument;
Database m_database = doc.Database;
Editor ed = doc.Editor;
FileInfo[] files = GetDWGFiles(m_database, ed, null);
string DirectoryPath = CreateDirectory(m_database, ed);
foreach(FileInfo file in files)
{
if (Path.GetFileName(m_database.OriginalFileName)!= file.Name)
{
System.IO.File.Copy(m_database.OriginalFileName, DirectoryPath + @"\" + file.Name, true);
CopyModelSpace(file.FullName, ed, DirectoryPath + @"\" + file.Name);
}
}
}
public static string CreateDirectory(Database database, Editor editor)
{
string path = Path.GetDirectoryName(database.OriginalFileName)+ @"\NewFiles";
if (Directory.Exists(path))
{
editor.WriteMessage("That Directory Already exist");
return null;
}
try
{
DirectoryInfo di = Directory.CreateDirectory(path);
editor.WriteMessage("\nThe directory was successfully created");
return path;
}
catch
{
editor.WriteMessage("\nError creating directory");
return null;
}
}
public static FileInfo[] GetDWGFiles(Database database, Editor editor, string path)
{
if (path == null)
{
path = Path.GetDirectoryName(database.OriginalFileName);
}
path = Path.GetDirectoryName(database.OriginalFileName);
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fileInfos = di.GetFiles("*.dwg");
if(fileInfos!= null )
{
return fileInfos;
}
else
{
return null;
}
}
public static void CopyingPaperSpace(string sourcefile, string TemplateDatabase, Editor editor, string destinationfileName)
{
Database sourcedb = new Database(false, true);
using (sourcedb)
{
try
{
sourcedb.ReadDwgFile(sourcefile, System.IO.FileShare.Read, false, "");
}
catch (System.Exception)
{
editor.WriteMessage("\n Unable to read drawing file.");
}
using (Transaction sourcetr = sourcedb.TransactionManager.StartTransaction())
{
ObjectId dId;
Database destinationdb = new Database(false, true);
using (destinationdb){
try
{
destinationdb.ReadDwgFile(TemplateDatabase, FileShare.Read, false, "");
}
catch(System.Exception)
{
editor.WriteMessage("\n Unable to read drawing file.");
}
using (Transaction destinationtr = destinationdb.TransactionManager.StartTransaction())
{
DBDictionary dlayouts = (DBDictionary)destinationtr.GetObject(destinationdb.LayoutDictionaryId, OpenMode.ForRead);
DBDictionary slayouts = (DBDictionary)sourcetr.GetObject(sourcedb.LayoutDictionaryId, OpenMode.ForRead);
if (slayouts.Contains("Layout1"))
{
ObjectIdCollection SourceIds = new ObjectIdCollection();
ObjectId sid = slayouts.GetAt("Layout1");
Layout slayout = (Layout)sourcetr.GetObject(sid, OpenMode.ForRead);
BlockTableRecord bt = (BlockTableRecord)sourcetr.GetObject(slayout.BlockTableRecordId, OpenMode.ForRead);
foreach (ObjectId objectId in bt)
{
SourceIds.Add(objectId);
}
BlockReference blkref = CreateBlock(sourcetr, SourceIds, sourcedb);
if (blkref == null)
{
editor.WriteMessage("\nNo se ha podido crear un bloque a partir del DWG actual ");
return;
}
SourceIds.Clear();
SourceIds.Add(blkref.Id);
IdMapping mapping = new IdMapping();
if (dlayouts.Contains("DrawingSheet"))
{
dId = dlayouts.GetAt("DrawingSheet");
sourcedb.WblockCloneObjects(SourceIds, dId, mapping, DuplicateRecordCloning.Replace, false);
destinationdb.SaveAs(destinationfileName, DwgVersion.Current);
}
else
{
editor.WriteMessage("\nNo se ha encontrado el espacio de papel en el archivo de destino");
return;
}
}
}
}
}
}
}
public static void CopyModelSpace(string sourcefile, Editor editor, string destinationfile)
{
Database sourcedb = new Database(false, true);
using (sourcedb)
{
try
{
sourcedb.ReadDwgFile(sourcefile, System.IO.FileShare.Read, false, "");
}
catch (System.Exception)
{
editor.WriteMessage("\n Unable to read drawing file.");
}
using (Transaction sourcetr = sourcedb.TransactionManager.StartTransaction())
{
Database destinationdb = new Database(false, true);
using (destinationdb)
{
try
{
destinationdb.ReadDwgFile(destinationfile, FileShare.ReadWrite, true, "");
}
catch (System.Exception)
{
editor.WriteMessage("\n Unable to read drawing file.");
}
ObjectId sourceMSId = SymbolUtilityServices.GetBlockModelSpaceId(sourcedb);
ObjectId destMSId = SymbolUtilityServices.GetBlockModelSpaceId(destinationdb);
ObjectIdCollection SourceIds = new ObjectIdCollection();
using (Transaction tr = sourcedb.TransactionManager.StartTransaction())
{
BlockTableRecord ms = (BlockTableRecord)tr.GetObject(sourceMSId, OpenMode.ForRead);
foreach(ObjectId id in ms)
{
SourceIds.Add(id);
}
}
editor.WriteMessage("\n el numero de objetos en la colección es {0}", SourceIds.Count);
using (Transaction desttr = destinationdb.TransactionManager.StartTransaction())
{
BlockTable destBlockTable = (BlockTable)desttr.GetObject(destinationdb.BlockTableId, OpenMode.ForWrite);
BlockTableRecord destBlockTableReord = desttr.GetObject(destBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
IdMapping mapping = new IdMapping();
sourcedb.WblockCloneObjects(SourceIds, destMSId, mapping, DuplicateRecordCloning.Replace, false);
desttr.Commit();
}
}
}
}
}
public static BlockReference CreateBlock(Transaction tr, ObjectIdCollection Ids, Database db)
{
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
string blkName = "PaperSpaceTemplate";
if (!bt.Has(blkName))
{
BlockTableRecord btr = new BlockTableRecord();
btr.Name = blkName;
btr.Origin = new Point3d(0, 0, 0);
ObjectId btrId = bt.Add(btr);
tr.AddNewlyCreatedDBObject(btr, true);
foreach(ObjectId id in Ids)
{
Entity dbobj = (Entity)tr.GetObject(id, OpenMode.ForWrite);
btr.AppendEntity(dbobj);
tr.AddNewlyCreatedDBObject(dbobj, true);
}
BlockReference blkref = new BlockReference(btr.Origin, btr.ObjectId);
blkref.ScaleFactors = new Scale3d(1000,1000,1);
return blkref;
}
else
{
return null;
}
}
}
}
}everywhere i found use of WblockCloneObject, one of the databases is the one of the current drawing but that is not my case, i need to copy from a closed drawing into another closed drawing, maybe this could be the problem. The object selection from model space is worrking as i have set some dialog to show quantity of collections and it works fine.
I hope some one here could help me solve this problem.
Regards.
Solved! Go to Solution.

