.NET

.NET

Reply
*Roland Feletic
Message 1 of 11 (489 Views)

How to insert blockreference not in drawing

489 Views, 10 Replies
07-04-2005 11:41 AM
Hi,
how can i insert a blockreference when the block is not in the drawing
but in one of the supportpath?

Roland
Valued Contributor
netcai
Posts: 66
Registered: ‎07-22-2004
Message 2 of 11 (489 Views)

Re: How to insert blockreference not in drawing

07-04-2005 05:48 PM in reply to: *Roland Feletic
you can use objectdbx
*Roland Feletic
Message 3 of 11 (489 Views)

Re: How to insert blockreference not in drawing

07-04-2005 10:05 PM in reply to: *Roland Feletic
Is it not possible to do it with the Managed Wrapper Classes in C#?

Roland
*Albert Szilvasy
Message 4 of 11 (489 Views)

Re: How to insert blockreference not in drawing

07-05-2005 08:14 AM in reply to: *Roland Feletic
It is possible. In fact, it is also possible using the COM API. I'd
recommend that you use the COM API method InsertBlock and specify the file
name. You can do this from C#.

If you use the .NET API directly then you must do a find file on the file
name (HostApplicationServices.FindFile). Then open the drawing using
Database.ReadDwgFile and then finally insert it using Database.Insert. The
COM API does the exact same thing in hte InsertBlock method.

Albert

"Roland Feletic" wrote in message
news:4892457@discussion.autodesk.com...
Is it not possible to do it with the Managed Wrapper Classes in C#?

Roland
*Roland Feletic
Message 5 of 11 (489 Views)

Re: How to insert blockreference not in drawing

07-05-2005 11:27 AM in reply to: *Roland Feletic
Thank you, Albert.
I will have a look at this two ways.
First is a little bit easier, second is more interresting ;-)

Roland
Valued Contributor
netcai
Posts: 66
Registered: ‎07-22-2004
Message 6 of 11 (489 Views)

Re: How to insert blockreference not in drawing

07-06-2005 06:03 PM in reply to: *Roland Feletic
Albert,
I use .net api method ,but failed.
HostApplicationServices doesn't has FindFile property, I can read file, but I don't know how to use Database.Insert method,could you show some examples in detail.
*Albert Szilvasy
Message 7 of 11 (489 Views)

Re: How to insert blockreference not in drawing

07-07-2005 02:34 PM in reply to: *Roland Feletic
I'm not sure why you can't find FindFile. Here's an example anyway:

public class Testinsert
{
[CommandMethod("testinsert")]
static public void DoIt()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptResult res = ed.GetString("Give me a file to insert");
if (res.Status != PromptStatus.OK)
return;
string fname = res.StringResult;
if (!File.Exists(fname))
fname =
HostApplicationServices.Current.FindFile(fname,doc.Database,FindFileHint.Default);
using (Database db = new Database(false,false))
{
//read drawing
db.ReadDwgFile(fname,FileShare.Read,true,null);
using (Transaction t =
doc.TransactionManager.StartTransaction())
{
//insert it as a new block
ObjectId idBTR = doc.Database.Insert("test",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);
using (BlockReference bref = new
BlockReference(Point3d.Origin,idBTR))
{
btr.AppendEntity(bref);
t.AddNewlyCreatedDBObject(bref, true);
}
t.Commit();
}
}

}
}

wrote in message news:4894592@discussion.autodesk.com...
Albert,
I use .net api method ,but failed.
HostApplicationServices doesn't has FindFile property, I can read file, but
I don't know how to use Database.Insert method,could you show some examples
in detail.
Valued Contributor
netcai
Posts: 66
Registered: ‎07-22-2004
Message 8 of 11 (489 Views)

Re: How to insert blockreference not in drawing

07-07-2005 07:05 PM in reply to: *Roland Feletic
I run your code ,but a error appear.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Autodesk.AutoCAD.Runtime.Exception: eSelfReference


public class Testinsert
{
[CommandMethod("testinsert")]
static public void DoIt()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
//Editor ed = doc.Editor;
//PromptResult res = ed.GetString("Give me a file to insert");
//if (res.Status != PromptStatus.OK)
// return;
//string fname = res.StringResult;
string fname = @"E:\My Documents\临时\plan-NA.dwg";
if ( ! File.Exists(fname))
fname = HostApplicationServices.Current.FindFile(fname, doc.Database, FindFileHint.Default);
using (Database db = new Database(false, false))
{
//read drawing
db.ReadDwgFile(fname, FileShare.Read, true, null);
using (Transaction t =
doc.TransactionManager.StartTransaction())
{
//insert it as a new block
ObjectId idBTR = doc.Database.Insert("N_spot", 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);
using (BlockReference bref = new BlockReference(Point3d.Origin, idBTR))
{
btr.AppendEntity(bref);
t.AddNewlyCreatedDBObject(bref, true);
}
t.Commit();
}
}

}
}
*Albert Szilvasy
Message 9 of 11 (489 Views)

Re: How to insert blockreference not in drawing

07-07-2005 07:34 PM in reply to: *Roland Feletic
Make sure that you are not inserting the drawing into itself and that you
don't already have a block called "N_spot". The code I posted works well if
you run it on a newly created drawing.

ALbert
wrote in message news:4895903@discussion.autodesk.com...
I run your code ,but a error appear.
System.Reflection.TargetInvocationException: Exception has been thrown by
the target of an invocation. ---> Autodesk.AutoCAD.Runtime.Exception:
eSelfReference


public class Testinsert
{
[CommandMethod("testinsert")]
static public void DoIt()
{
Document doc =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
//Editor ed = doc.Editor;
//PromptResult res = ed.GetString("Give me a file to insert");
//if (res.Status != PromptStatus.OK)
// return;
//string fname = res.StringResult;
string fname = @"E:\My Documents\??\plan-NA.dwg";
if ( ! File.Exists(fname))
fname = HostApplicationServices.Current.FindFile(fname,
doc.Database, FindFileHint.Default);
using (Database db = new Database(false, false))
{
//read drawing
db.ReadDwgFile(fname, FileShare.Read, true, null);
using (Transaction t =
doc.TransactionManager.StartTransaction())
{
//insert it as a new block
ObjectId idBTR = doc.Database.Insert("N_spot", 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);
using (BlockReference bref = new
BlockReference(Point3d.Origin, idBTR))
{
btr.AppendEntity(bref);
t.AddNewlyCreatedDBObject(bref, true);
}
t.Commit();
}
}

}
}
*Roland Feletic
Message 10 of 11 (489 Views)

Re: How to insert blockreference not in drawing

07-07-2005 10:25 PM in reply to: *Roland Feletic
Hallo Albert,
thank you for your code, hope that i have a little more time after my
holiday.
Roland
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.