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

Insert External Drawing

0 REPLIES 0
Reply
Message 1 of 1
Anonymous
272 Views, 0 Replies

Insert External Drawing

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

}
0 REPLIES 0

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