Community
{code}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Acad = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof( Namespace1.InsertBlockFromFileExample ))]
namespace Namespace1
{
public static class InsertBlockFromFileExample
{
[CommandMethod("XINSERT")]
public static void InsertFromFileCommand()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.DefaultExt = ".dwg";
ofd.Filter = "Drawing Files (*.dwg)|*.dwg";
if( ofd.ShowDialog( Acad.MainWindow ) != DialogResult.OK )
return;
Document doc = Acad.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptPointOptions ppo = new PromptPointOptions( "\nInsertion
point: " );
PromptPointResult ppr = ed.GetPoint( ppo );
if( ppr.Status != PromptStatus.OK )
return;
using( Database xDb = new Database( false, true ) )
{
xDb.ReadDwgFile( ofd.FileName, FileShare.Read, true, null );
using( Transaction tr =
doc.TransactionManager.StartTransaction() )
{
string name =
SymbolUtilityServices.GetBlockNameFromInsertPathName( ofd.FileName );
ObjectId id = db.Insert( name, xDb, true );
if( id.IsNull )
{
ed.WriteMessage( "\nFailed to insert block" );
return;
}
BlockTableRecord currSpace = tr.GetObject( db.CurrentSpaceId,
OpenMode.ForWrite ) as BlockTableRecord;
BlockReference insert = new BlockReference( ppr.Value, id );
currSpace.AppendEntity( insert );
insert.SetDatabaseDefaults();
tr.AddNewlyCreatedDBObject( insert, true );
tr.Commit();
}
}
}
}
}
{code}
{code}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Internal;
using Acad = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof( RSNNAcadApp.Test.InsertBlock_Tanzillo ))]
namespace RSNNAcadApp.Test
{
class InsertBlock_Tanzillo
{
[CommandMethod("XINSERT")]
public static void InsertFromFileCommand()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.DefaultExt = ".dwg";
ofd.Filter = "Drawing Files (*.dwg)|*.dwg";
if (ofd.ShowDialog(Acad.MainWindow) != DialogResult.OK)
return;
Document doc = Acad.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptPointOptions ppo = new PromptPointOptions("\nEinsetzpunkt:
");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK)
return;
using (Database xDb = new Database(false, true))
{
xDb.ReadDwgFile(ofd.FileName, FileShare.Read, true, null);
using (Transaction tr =
doc.TransactionManager.StartTransaction())
{
string name =
SymbolUtilityServices.GetBlockNameFromInsertPathName(ofd.FileName);
ObjectId id = db.Insert(name, xDb, true);
if (id.IsNull)
{
ed.WriteMessage("\nFailed to insert block");
return;
}
//for inserting an annotative dwg
if (xDb.AnnotativeDwg)
{
using (BlockTableRecord btrAnnotative =
(BlockTableRecord)tr.GetObject(id, OpenMode.ForWrite))
{
btrAnnotative.Annotative =
AnnotativeStates.True;
}
}
BlockTableRecord currSpace =
tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
BlockReference insert = new BlockReference(ppr.Value,
id);
currSpace.AppendEntity(insert);
insert.SetDatabaseDefaults();
//for inserting an annotative dwg
if (xDb.AnnotativeDwg)
{
//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");
ObjectContexts.AddContext(insert,
occ.CurrentContext);
}
tr.AddNewlyCreatedDBObject(insert, true);
tr.Commit();
}
}
}
}
}
{code}
{code}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Internal;
using Acad = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof( RSNNAcadApp.Test.InsertBlock_Tanzillo ))]
namespace RSNNAcadApp.Test
{
class InsertBlock_Tanzillo
{
[CommandMethod("XINSERT")]
public static void InsertFromFileCommand()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.DefaultExt = ".dwg";
ofd.Filter = "Drawing Files (*.dwg)|*.dwg";
if (ofd.ShowDialog(Acad.MainWindow) != DialogResult.OK)
return;
Document doc = Acad.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptPointOptions ppo = new PromptPointOptions("\nEinsetzpunkt:
");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK)
return;
using (Database xDb = new Database(false, true))
{
xDb.ReadDwgFile(ofd.FileName, FileShare.Read, true, null);
using (Transaction tr =
doc.TransactionManager.StartTransaction())
{
string name =
SymbolUtilityServices.GetBlockNameFromInsertPathName(ofd.FileName);
ObjectId id = db.Insert(name, xDb, true);
if (id.IsNull)
{
ed.WriteMessage("\nFailed to insert block");
return;
}
//for inserting an annotative dwg
if (xDb.AnnotativeDwg)
{
using (BlockTableRecord btrAnnotative =
(BlockTableRecord)tr.GetObject(id, OpenMode.ForWrite))
{
btrAnnotative.Annotative =
AnnotativeStates.True;
}
}
BlockTableRecord currSpace =
tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
BlockReference insert = new BlockReference(ppr.Value,
id);
currSpace.AppendEntity(insert);
insert.SetDatabaseDefaults();
//for inserting an annotative dwg
if (xDb.AnnotativeDwg)
{
//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");
ObjectContexts.AddContext(insert,
occ.CurrentContext);
}
tr.AddNewlyCreatedDBObject(insert, true);
tr.Commit();
}
}
}
}
}
{code}
Can't find what you're looking for? Ask the community or share your knowledge.