.NET

.NET

Reply
Mentor
242 Posts
16 Kudos
Registered: ‎12-15-2011
Post 1 of 4

Insert a Drawing to a specific Coordinate

292 Views, 3 Replies
09-24-2012 03:42 AM

Am trying to insert a drawing to specific coordinates, till now i did this part of coding; though this is executing but the drawing is not getting to coordinates specified

 

public void coordinates()
{
string dwgName = HostApplicationServices.Current.FindFile("D:\\AutoCAD DWG's\\SE-02416_37_A.dwg", acadApp.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default);
//Database db1 = new Database(false, false);
Database db1 = new Database(false, false);
//db1.ReadDwgFile(dwgName, System.IO.FileShare.Write, true, "");
db1.ReadDwgFile(dwgName, FileOpenMode.OpenForReadAndWriteNoShare, true, "");

ObjectId BlkId;
Document doc = acadApp.DocumentManager.MdiActiveDocument;

Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
BlkId = doc.Database.Insert(dwgName, db1, false);
BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite, true);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
BlockReference bref = new BlockReference(new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0), BlkId);
btr.AppendEntity(bref);
tr.AddNewlyCreatedDBObject(bref, true);
bref.ExplodeToOwnerSpace();

bref.Erase();
tr.Commit();
tr.Dispose();
}
}

 

Can some one tell me where am i going wrong

*Expert Elite*
1,088 Posts
217 Kudos
Registered: ‎04-27-2009
Post 2 of 4

Re: Insert a Drawing to a specific Coordinate

09-24-2012 06:55 AM in reply to: amitk_189

The similar situation has been posted here and answered before. I think your problem is not the inserted block reference not being in the location of specified coordinate (0,0,0), it is that the block is nowhere to find.

 

There reason is here:

 

BlkId = doc.Database.Insert(dwgName, db1, false);

 

Note the first argument of the Insert() method requires a valid block name, while you pass in a full file path, meaning that the string includes something like "\", which is not allowed in e block name. When you do manual block definition, AutoCAD's BLOCK dialog box will validate block name and reject if the name contains invalid character, however, in .NET API, if a block definition is created by Database.Insert(), it is entirely up to programmer to make sure the block name does not contain invalid character. If it dies contain onvalid character, such as in your case, the block definition is still created, but the block name is BLANK! (you can write code to search the BlockTableRecord after your Database.Insert() to see what I am saying). Then it would cause the situation where the block reference based on this "BLANK" named block definition is inserted into nowhere.

 

So, you need to use the block file's file name without path/extension in the Database.Insert():

 

doc.Database.Insert(System.IO.Path.GetFileNameWithoutExtension(dwgName), db1, false)

 

HTH

Mentor
242 Posts
16 Kudos
Registered: ‎12-15-2011
Post 3 of 4

Re: Insert a Drawing to a specific Coordinate

09-24-2012 09:31 PM in reply to: amitk_189

Hello norman.yuan, I did as you said; its still inserting data in -179,-180,0.

I am trying to get this to 0,0,0 its still the same

 

Regards

Amit

Valued Mentor
278 Posts
22 Kudos
Registered: ‎11-29-2008
Post 4 of 4

Re: Insert a Drawing to a specific Coordinate

09-25-2012 12:04 AM in reply to: amitk_189

amitk_189 wrote:

Hello norman.yuan, I did as you said; its still inserting data in -179,-180,0.

I am trying to get this to 0,0,0 its still the same

 

Regards

Amit


 

What is the BASEPOINT variable in your template drawing. ??

 

I can think of no reason why the insertion point should change.

 

Have you tried the process inserting a different drawing ??

 

 

 

//-------------------------------------------------------

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!