.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to insert blockreference not in drawing

10 REPLIES 10
Reply
Message 1 of 11
Anonymous
862 Views, 10 Replies

How to insert blockreference not in drawing

Hi,
how can i insert a blockreference when the block is not in the drawing
but in one of the supportpath?

Roland
10 REPLIES 10
Message 2 of 11
netcai
in reply to: Anonymous

you can use objectdbx
Message 3 of 11
Anonymous
in reply to: Anonymous

Is it not possible to do it with the Managed Wrapper Classes in C#?

Roland
Message 4 of 11
Anonymous
in reply to: Anonymous

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
Message 5 of 11
Anonymous
in reply to: Anonymous

Thank you, Albert.
I will have a look at this two ways.
First is a little bit easier, second is more interresting 😉

Roland
Message 6 of 11
netcai
in reply to: Anonymous

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.
Message 7 of 11
Anonymous
in reply to: Anonymous

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.
Message 8 of 11
netcai
in reply to: Anonymous

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();
}
}

}
}
Message 9 of 11
Anonymous
in reply to: Anonymous

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();
}
}

}
}
Message 10 of 11
Anonymous
in reply to: Anonymous

Hallo Albert,
thank you for your code, hope that i have a little more time after my
holiday.
Roland
Message 11 of 11
netcai
in reply to: Anonymous

thanks ,I se.
but how to copy a block from a unopened file to current document. I tried use
ObjectId idBTR = doc.Database.Insert( sourceBlockname,destinationBlockName, db, false), but failed.A fatal error accured,

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost