.NET

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

Insert External Drawing

98 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
{
}
}
}

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