.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Copy all entities from an existing drawing

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
SRSDS
3587 Views, 8 Replies

Copy all entities from an existing drawing

Hi,

 

Can I ask for code to import all entities from an existing drawing into the current one. I get tired of opening our standard details and copying and pasting the company's standard layers, linetypes etc.

8 REPLIES 8
Message 2 of 9
khoa.ho
in reply to: SRSDS

Use db.WblockCloneObjects to copy/clone entities from source to destination drawing. Here is the code to import all entities from your standard drawing to the current drawing. The code copies only entities in model space including blocks, so you should have the entity storing what needed for company standards (layers, linetypes...)

 

[CommandMethod("ImportDrawing")]
public static void ImportDrawing()
{
    string drawingPath = @"C:\Temp\StandardTemplate.dwg";
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        try
        {
            var objectIds = new ObjectIdCollection();
            var blockTable = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
            var extDb = new Database();
            extDb.ReadDwgFile(drawingPath, System.IO.FileShare.Read, false, null);
            using (Transaction extTrans = extDb.TransactionManager.StartTransaction())
            {
                var extBlockTable =
                    (BlockTable)extTrans.GetObject(
                        extDb.BlockTableId, OpenMode.ForRead);
                var extModelSpace =
                    (BlockTableRecord)extTrans.GetObject(
                        extBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                foreach (ObjectId id in extModelSpace)
                {
                    objectIds.Add(id);
                }
            }
            extDb.CloseInput(true);
            var idMapping = new IdMapping();
            db.WblockCloneObjects(
                objectIds,
                blockTable[BlockTableRecord.ModelSpace],
                idMapping,
                DuplicateRecordCloning.Replace,
                false);
            trans.Commit();
        }
        catch (System.Exception ex)
        {
        }
    }
}

 

Message 3 of 9
SRSDS
in reply to: khoa.ho

Thank you.

 

I'm getting an ePermanentlyErased error on the line where it tries to clone the objects. I've tried to search for an answer but I can't seem to find one.

Would you know why this would be occurring? I don't think the source drawing is the problem. There's not a lot in it.

 

  db.WblockCloneObjects(
                objectIds,
                blockTable[BlockTableRecord.ModelSpace],
                idMapping,
                DuplicateRecordCloning.Replace,
                false);
Message 4 of 9
HJohn1
in reply to: SRSDS

Have you tried moving the line "extDb.CloseInput(true);" below the call to db.WblockCloneObjects method?

Message 5 of 9
SRSDS
in reply to: HJohn1

After moving that line I get an eOutOfRange error.

Message 6 of 9
jeff
in reply to: SRSDS

Might be easier to just insert the drawing then explode.

You can also find your answers @ TheSwamp
Message 7 of 9
khoa.ho
in reply to: SRSDS

Coming from ePermanentlyErased I guess you should need to check if this entity was erased or not. ObjectId.IsErased may be the one you need.

 

Here is the updated code:

 

[CommandMethod("ImportDrawing")]
public static void ImportDrawing()
{
    string drawingPath = @"C:\Temp\StandardTemplate.dwg";
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        try
        {
            var objectIds = new ObjectIdCollection();
            var blockTable = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
            var extDb = new Database();
            extDb.ReadDwgFile(drawingPath, System.IO.FileShare.Read, false, null);
            using (Transaction extTrans = extDb.TransactionManager.StartTransaction())
            {
                var extBlockTable =
                    (BlockTable)extTrans.GetObject(extDb.BlockTableId, OpenMode.ForRead);
                var extModelSpace =
                    (BlockTableRecord)extTrans.GetObject(extBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                foreach (ObjectId id in extModelSpace)
                {
                    if (!id.IsEffectivelyErased &&
                        !id.IsErased &&
                        id.IsValid)
                        objectIds.Add(id);
                }
            }
            extDb.CloseInput(true);
            var idMapping = new IdMapping();
            db.WblockCloneObjects(
                objectIds,
                blockTable[BlockTableRecord.ModelSpace],
                idMapping,
                DuplicateRecordCloning.Replace,
                false);
            trans.Commit();
        }
        catch (System.Exception ex)
        {
        }
    }
}

  

You can use extDb.CloseInput(true) either before or after transaction commit. I tested both work. The reason I put it before db.WblockCloneObjects because this method may sometimes jump out errors and I will miss the chance to close the open imported drawing.

 

Message 8 of 9
SRSDS
in reply to: khoa.ho

 

There was no change in that last modification. Same error.

 

TBH I don't really understand every line of code but I compared it to another routine I had for importing blocks and pieced this together which works.

Please note that I have converted it back from VB.NET to C# for consistency. I can't be sure how the translator did.

 

Thankyou very much for your help!!

 

[CommandMethod("ImportDrawing")]
public static void ImportDrawing()
{
	string drawingPath = "C:\Temp\StandardTemplate.dwg";
	Database destDb = Application.DocumentManager.MdiActiveDocument.Database;
	Database sourceDb = new Database(false, true);
	sourceDb.ReadDwgFile(drawingPath, System.IO.FileShare.Read, false, null);
	dynamic SourceObjectIds = new ObjectIdCollection();
	Autodesk.AutoCAD.DatabaseServices.TransactionManager SourceTM = sourceDb.TransactionManager;
	using (Transaction trans = destDb.TransactionManager.StartTransaction()) {
		try {
			using (Transaction extTrans = sourceDb.TransactionManager.StartTransaction()) {
				dynamic extBlockTable = (BlockTable)extTrans.GetObject(sourceDb.BlockTableId, OpenMode.ForRead);
				dynamic extModelSpace = (BlockTableRecord)extTrans.GetObject(extBlockTable(BlockTableRecord.ModelSpace), OpenMode.ForRead);
				foreach (ObjectId id in extModelSpace) {
					SourceObjectIds.Add(id);
				}
			}
			sourceDb.CloseInput(true);
			dynamic idMapping = new IdMapping();
			destDb.WblockCloneObjects(SourceObjectIds, destDb.BlockTableId, idMapping, DuplicateRecordCloning.Replace, false);
			trans.Commit();
		} catch (System.Exception ex) {
			Interaction.MsgBox("Exception:" + ex.Message);
		}
	}
	sourceDb.Dispose();
}

 

 

Message 9 of 9
SENL1362
in reply to: jeff

Agree! It also copies more than just Model objects, such as PlotStyles

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost