.NET

Reply
Distinguished Contributor
Posts: 172
Registered: ‎11-19-2003
Message 1 of 1 (92 Views)

Insert External Drawing

92 Views, 0 Replies
05-09-2007 06:20 AM
I want to insert an external drawing as a block and make it annotative.
Something is wrong with this code, I allways get a fatatl error after
inserting the block. Does anyone know what is wrong with the attached code?


#region Using directives

using System.IO;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Internal;

#endregion

[assembly: CommandClass(typeof(RSNNAcadApp.Utils.Block.Testinsert))]
namespace RSNNAcadApp.Utils.Block
{
public class Testinsert
{
[CommandMethod("testinsert")]
static public void DoIt()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
try
{
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))
{
ed.WriteMessage(System.String.Format("\nFilename 1:
{0}", fname));

//read drawing
db.ReadDwgFile(fname, FileShare.Read, true, null);
using (Transaction t =
doc.TransactionManager.StartTransaction())
{
ed.WriteMessage(System.String.Format("\nFilename 2:
{0}", db.Filename));
//insert it as a new block
ObjectId idBTR = doc.Database.Insert("test", db,
true);
//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))
{
ObjectId obj = btr.AppendEntity(bref);
t.AddNewlyCreatedDBObject(bref, true);


// Add current annotationscale if the block is
annotative
#region Add Annotation Scale
using (BlockTableRecord tmpAnnoBlock =
(BlockTableRecord)t.GetObject(idBTR, OpenMode.ForWrite))
{
tmpAnnoBlock.Annotative =
AnnotativeStates.True;
tmpAnnoBlock.UpgradeOpen();

if (tmpAnnoBlock.Annotative ==
AnnotativeStates.True)
{
ed.WriteMessage(string.Format("Block ist
annotative: {0}", tmpAnnoBlock.Annotative.ToString()));
//Attach Current Annotation-Scale.
//If you don't add the content, the
block and the following attribute will not inserted correct.
ObjectContextManager ocm =
db.ObjectContextManager;
ObjectContextCollection occ =
ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");

DBObject AnnoObj = t.GetObject(obj,
OpenMode.ForRead);
if (AnnoObj != null)
{
//ObjectContexts.AddContext(obj,
occ.GetContext("1:1"));
ObjectContexts.AddContext(AnnoObj,
occ.CurrentContext);
}
}
}
#endregion
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}

t.Commit();
}

}

}
catch
{
ed.WriteMessage("Nothing to do ;-)");
}
finally
{
}
}
}

}

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community