.NET

.NET

Reply
Mentor
amitk_189
Posts: 242
Registered: ‎12-15-2011
Message 1 of 4 (289 Views)

Insert a Drawing to a specific Coordinate

289 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*
norman.yuan
Posts: 1,063
Registered: ‎04-27-2009
Message 2 of 4 (275 Views)

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
amitk_189
Posts: 242
Registered: ‎12-15-2011
Message 3 of 4 (255 Views)

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
KerryBrown
Posts: 278
Registered: ‎11-29-2008
Message 4 of 4 (242 Views)

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
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.