.NET

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

Insert Method

234 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 (234 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 (234 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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.