.NET

Reply
Member
steve
Posts: 5
Registered: ‎06-18-2002
Message 1 of 3 (232 Views)

Insert Method

232 Views, 2 Replies
07-20-2005 05:52 AM
Can someone give me an example of how you would insert the modelspace entities from an external drawing file into the active drawing with .net?

Thanks,

Steve
*Carlos
Message 2 of 3 (232 Views)

Re: Insert Method

07-20-2005 10:20 AM in reply to: steve
Hello,

I use C# with VS 2003 and AutoCAD 2005. I haven't quite figured out how to
write attributes to it yet, but this is a start (the code is pasted at the
end).

Does anyone out there know of a way to call AutoCAD commands from C#? In
ObjectARX C++ there was the acedCommand method, so I could use:
acedCommand(RTSTR, "-INSERT", RTSTR, "My Block", ... etc etc)

It would be nice to have that again.

Hope this helps. I'm curious about the other responses too.

-Carlos

public void InsertIntoAutoCAD()

{

// Insert the block into the AutoCAD model

try

{

Document doc = Application.DocumentManager.MdiActiveDocument;

string fname = "C:\\Blocks\\My Block.dwg";

using (Database db = new Database(false, false))

{

//read block drawing

db.ReadDwgFile(fname, System.IO.FileShare.Read, true, null);

using (Transaction t = doc.TransactionManager.StartTransaction())

{

//insert the new block

Autodesk.AutoCAD.ApplicationServices.CommandLinePrompts.Message("\n
Inserting the block...");

ObjectId idBTR = doc.Database.Insert("My Block",db,false);

//create a ref to the block

BlockTable bt = (BlockTable)t.GetObject(doc.Database.BlockTableId,
OpenMode.ForRead);

BlockTableRecord btr =
(BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite);

Autodesk.AutoCAD.Geometry.Point3d location = new Point3d(0.0, 0.0, 0.0);

using (BlockReference bref = new BlockReference(location, idBTR))

{

btr.AppendEntity(bref);

t.TransactionManager.AddNewlyCreatedDBObject(bref, true);

t.Commit();

}

}

}

catch

{

}

}

wrote in message news:4906068@discussion.autodesk.com...
Can someone give me an example of how you would insert the modelspace
entities from an external drawing file into the active drawing with .net?

Thanks,

Steve
*Carlos
Message 3 of 3 (232 Views)

Re: Insert Method

07-20-2005 10:49 AM in reply to: steve
Never mind...

I guess you can't. I had missed the other posts on the acedCommand topic.

-Carlos

"Carlos" wrote in message
news:4906567@discussion.autodesk.com...
Hello,

I use C# with VS 2003 and AutoCAD 2005. I haven't quite figured out how to
write attributes to it yet, but this is a start (the code is pasted at the
end).

Does anyone out there know of a way to call AutoCAD commands from C#? In
ObjectARX C++ there was the acedCommand method, so I could use:
acedCommand(RTSTR, "-INSERT", RTSTR, "My Block", ... etc etc)

It would be nice to have that again.

Hope this helps. I'm curious about the other responses too.

-Carlos

public void InsertIntoAutoCAD()

{

// Insert the block into the AutoCAD model

try

{

Document doc = Application.DocumentManager.MdiActiveDocument;

string fname = "C:\\Blocks\\My Block.dwg";

using (Database db = new Database(false, false))

{

//read block drawing

db.ReadDwgFile(fname, System.IO.FileShare.Read, true, null);

using (Transaction t = doc.TransactionManager.StartTransaction())

{

//insert the new block

Autodesk.AutoCAD.ApplicationServices.CommandLinePrompts.Message("\n
Inserting the block...");

ObjectId idBTR = doc.Database.Insert("My Block",db,false);

//create a ref to the block

BlockTable bt = (BlockTable)t.GetObject(doc.Database.BlockTableId,
OpenMode.ForRead);

BlockTableRecord btr =
(BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite);

Autodesk.AutoCAD.Geometry.Point3d location = new Point3d(0.0, 0.0, 0.0);

using (BlockReference bref = new BlockReference(location, idBTR))

{

btr.AppendEntity(bref);

t.TransactionManager.AddNewlyCreatedDBObject(bref, true);

t.Commit();

}

}

}

catch

{

}

}

wrote in message news:4906068@discussion.autodesk.com...
Can someone give me an example of how you would insert the modelspace
entities from an external drawing file into the active drawing with .net?

Thanks,

Steve
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.