[CommandMethod("AttachRasterImage")] publicvoidAttachRasterImage() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Define the name and image to use string strImgName = "WorldMap"; string strFileName = "C:\\AutoCAD\\Sample\\VBA\\WorldMap.TIF";
// Get the image dictionary ObjectId acImgDctID = RasterImageDef.GetImageDictionary(acCurDb);
// Check to see if the dictionary does not exist, it not then create it if (acImgDctID.IsNull) { acImgDctID = RasterImageDef.CreateImageDictionary(acCurDb); }
// Open the image dictionary DBDictionary acImgDict = acTrans.GetObject(acImgDctID, OpenMode.ForRead) as DBDictionary;
// Check to see if the image definition already exists if (acImgDict.Contains(strImgName)) { acImgDefId = acImgDict.GetAt(strImgName);
acRasterDef = acTrans.GetObject(acImgDefId, OpenMode.ForWrite) as RasterImageDef; } else { // Create a raster image definition RasterImageDef acRasterDefNew = new RasterImageDef();
// Set the source for the image file acRasterDefNew.SourceFileName = strFileName;
// Load the image into memory acRasterDefNew.Load();
// Add the image definition to the dictionary acTrans.GetObject(acImgDctID, OpenMode.ForWrite); acImgDefId = acImgDict.SetAt(strImgName, acRasterDefNew);
// Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
// Create the new image and assign it the image definition using (RasterImage acRaster = new RasterImage()) { acRaster.ImageDefId = acImgDefId;
// Use ImageWidth and ImageHeight to get the size of the image in pixels (1024 x 768). // Use ResolutionMMPerPixel to determine the number of millimeters in a pixel so you // can convert the size of the drawing into other units or millimeters based on the // drawing units used in the current drawing.
// Define the width and height of the image Vector3d width; Vector3d height;
// Check to see if the measurement is set to English (Imperial) or Metric units if (acCurDb.Measurement == MeasurementValue.English) { width = new Vector3d((acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth) / 25.4, 0, 0); height = new Vector3d(0, (acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight) / 25.4, 0); } else { width = new Vector3d(acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth, 0, 0); height = new Vector3d(0, acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight, 0); }
// Define the position for the image Point3d insPt = new Point3d(5.0, 5.0, 0.0);
// Define and assign a coordinate system for the image's orientation CoordinateSystem3d coordinateSystem = new CoordinateSystem3d(insPt, width * 2, height * 2); acRaster.Orientation = coordinateSystem;
// Set the rotation angle for the image acRaster.Rotation = 0;
// Add the new object to the block table record and the transaction acBlkTblRec.AppendEntity(acRaster); acTrans.AddNewlyCreatedDBObject(acRaster, true);
// Connect the raster definition and image together so the definition // does not appear as "unreferenced" in the External References palette. RasterImage.EnableReactors(true); acRaster.AssociateRasterDef(acRasterDef);
if (bRasterDefCreated) { acRasterDef.Dispose(); } }
// Save the new object to the database acTrans.Commit();
[CommandMethod("ClippingRasterBoundary")] publicvoidClippingRasterBoundary() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Define the name and image to use string strImgName = "WorldMap"; string strFileName = "C:\\AutoCAD\\Sample\\VBA\\WorldMap.TIF";
// Get the image dictionary ObjectId acImgDctID = RasterImageDef.GetImageDictionary(acCurDb);
// Check to see if the dictionary does not exist, it not then create it if (acImgDctID.IsNull) { acImgDctID = RasterImageDef.CreateImageDictionary(acCurDb); }
// Open the image dictionary DBDictionary acImgDict = acTrans.GetObject(acImgDctID, OpenMode.ForRead) as DBDictionary;
// Check to see if the image definition already exists if (acImgDict.Contains(strImgName)) { acImgDefId = acImgDict.GetAt(strImgName);
acRasterDef = acTrans.GetObject(acImgDefId, OpenMode.ForWrite) as RasterImageDef; } else { // Create a raster image definition RasterImageDef acRasterDefNew = new RasterImageDef();
// Set the source for the image file acRasterDefNew.SourceFileName = strFileName;
// Load the image into memory acRasterDefNew.Load();
// Add the image definition to the dictionary acTrans.GetObject(acImgDctID, OpenMode.ForWrite); acImgDefId = acImgDict.SetAt(strImgName, acRasterDefNew);
// Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
// Create the new image and assign it the image definition using (RasterImage acRaster = new RasterImage()) { acRaster.ImageDefId = acImgDefId;
// Use ImageWidth and ImageHeight to get the size of the image in pixels (1024 x 768). // Use ResolutionMMPerPixel to determine the number of millimeters in a pixel so you // can convert the size of the drawing into other units or millimeters based on the // drawing units used in the current drawing.
// Define the width and height of the image Vector3d width; Vector3d height;
// Check to see if the measurement is set to English (Imperial) or Metric units if (acCurDb.Measurement == MeasurementValue.English) { width = new Vector3d((acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth) / 25.4, 0, 0); height = new Vector3d(0, (acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight) / 25.4, 0); } else { width = new Vector3d(acRasterDef.ResolutionMMPerPixel.X * acRaster.ImageWidth, 0, 0); height = new Vector3d(0, acRasterDef.ResolutionMMPerPixel.Y * acRaster.ImageHeight, 0); }
// Define the position for the image Point3d insPt = new Point3d(5.0, 5.0, 0.0);
// Define and assign a coordinate system for the image's orientation CoordinateSystem3d coordinateSystem = new CoordinateSystem3d(insPt, width * 2, height * 2); acRaster.Orientation = coordinateSystem;
// Set the rotation angle for the image acRaster.Rotation = 0;
// Clip the image acRaster.SetClipBoundary(ClipBoundaryType.Rectangle, acPt2dColl);
// Enable the display of the clip acRaster.IsClipped = true;
// Add the new object to the block table record and the transaction acBlkTblRec.AppendEntity(acRaster); acTrans.AddNewlyCreatedDBObject(acRaster, true);
// Connect the raster definition and image together so the definition // does not appear as "unreferenced" in the External References palette. RasterImage.EnableReactors(true); acRaster.AssociateRasterDef(acRasterDef);
if (bRasterDefCreated) { acRasterDef.Dispose(); } }
// Save the new object to the database acTrans.Commit();
[CommandMethod("CreatingABlock")] publicvoidCreatingABlock() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!acBlkTbl.Has("CircleBlock")) { using (BlockTableRecord acBlkTblRec = new BlockTableRecord()) { acBlkTblRec.Name = "CircleBlock";
// Set the insertion point for the block acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block using (Circle acCirc = new Circle()) { acCirc.Center = new Point3d(0, 0, 0); acCirc.Radius = 2;
[CommandMethod("InsertingABlock")] publicvoidInsertingABlock() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if (!acBlkTbl.Has("CircleBlock")) { using (BlockTableRecord acBlkTblRec = new BlockTableRecord()) { acBlkTblRec.Name = "CircleBlock";
// Set the insertion point for the block acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block using (Circle acCirc = new Circle()) { acCirc.Center = new Point3d(0, 0, 0); acCirc.Radius = 2;
// Insert the block into the current space if (blkRecId != ObjectId.Null) { using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), blkRecId)) { BlockTableRecord acCurSpaceBlkTblRec; acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
[CommandMethod("ExplodingABlock")] publicvoidExplodingABlock() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if (!acBlkTbl.Has("CircleBlock")) { using (BlockTableRecord acBlkTblRec = new BlockTableRecord()) { acBlkTblRec.Name = "CircleBlock";
// Set the insertion point for the block acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block using (Circle acCirc = new Circle()) { acCirc.Center = new Point3d(0, 0, 0); acCirc.Radius = 2;
// Insert the block into the current space if (blkRecId != ObjectId.Null) { using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), blkRecId)) { BlockTableRecord acCurSpaceBlkTblRec; acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
[CommandMethod("RedefiningABlock")] publicvoidRedefiningABlock() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!acBlkTbl.Has("CircleBlock")) { using (BlockTableRecord acBlkTblRec = new BlockTableRecord()) { acBlkTblRec.Name = "CircleBlock";
// Set the insertion point for the block acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block using (Circle acCirc = new Circle()) { acCirc.Center = new Point3d(0, 0, 0); acCirc.Radius = 2;
// Insert the block into the current space using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), acBlkTblRec.Id)) { BlockTableRecord acModelSpace; acModelSpace = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
Application.ShowAlertDialog("CircleBlock has been created."); } } } } else { // Redefine the block if it exists BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl["CircleBlock"], OpenMode.ForWrite) as BlockTableRecord;
// Step through each object in the block table record foreach (ObjectId objID in acBlkTblRec) { DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;
// Revise the circle in the block if (dbObj is Circle) { Circle acCirc = dbObj as Circle;
[CommandMethod("AddingAttributeToABlock")] publicvoidAddingAttributeToABlock() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!acBlkTbl.Has("CircleBlockWithAttributes")) { using (BlockTableRecord acBlkTblRec = new BlockTableRecord()) { acBlkTblRec.Name = "CircleBlockWithAttributes";
// Set the insertion point for the block acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block using (Circle acCirc = new Circle()) { acCirc.Center = new Point3d(0, 0, 0); acCirc.Radius = 2;
acBlkTblRec.AppendEntity(acCirc);
// Add an attribute definition to the block using (AttributeDefinition acAttDef = new AttributeDefinition()) { acAttDef.Position = new Point3d(0, 0, 0); acAttDef.Verifiable = true; acAttDef.Prompt = "Door #: "; acAttDef.Tag = "Door#"; acAttDef.TextString = "DXX"; acAttDef.Height = 1; acAttDef.Justify = AttachmentPoint.MiddleCenter;
[CommandMethod("InsertingBlockWithAnAttribute")] publicvoidInsertingBlockWithAnAttribute() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if (!acBlkTbl.Has("CircleBlockWithAttributes")) { using (BlockTableRecord acBlkTblRec = new BlockTableRecord()) { acBlkTblRec.Name = "CircleBlockWithAttributes";
// Set the insertion point for the block acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block using (Circle acCirc = new Circle()) { acCirc.Center = new Point3d(0, 0, 0); acCirc.Radius = 2;
acBlkTblRec.AppendEntity(acCirc);
// Add an attribute definition to the block using (AttributeDefinition acAttDef = new AttributeDefinition()) { acAttDef.Position = new Point3d(0, 0, 0); acAttDef.Prompt = "Door #: "; acAttDef.Tag = "Door#"; acAttDef.TextString = "DXX"; acAttDef.Height = 1; acAttDef.Justify = AttachmentPoint.MiddleCenter; acBlkTblRec.AppendEntity(acAttDef);
// Insert the block into the current space if (blkRecId != ObjectId.Null) { BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
// Create and insert the new block reference using (BlockReference acBlkRef = new BlockReference(new Point3d(2, 2, 0), blkRecId)) { BlockTableRecord acCurSpaceBlkTblRec; acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
// Verify block table record has attribute definitions associated with it if (acBlkTblRec.HasAttributeDefinitions) { // Add attributes from the block table record foreach (ObjectId objID in acBlkTblRec) { DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;
if (dbObj is AttributeDefinition) { AttributeDefinition acAtt = dbObj as AttributeDefinition;
if (!acAtt.Constant) { using (AttributeReference acAttRef = new AttributeReference()) { acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform); acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
[CommandMethod("RedefiningAnAttribute")] publicvoidRedefiningAnAttribute() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if (!acBlkTbl.Has("CircleBlockWithAttributes")) { using (BlockTableRecord acBlkTblRec = new BlockTableRecord()) { acBlkTblRec.Name = "CircleBlockWithAttributes";
// Set the insertion point for the block acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block using (Circle acCirc = new Circle()) { acCirc.Center = new Point3d(0, 0, 0); acCirc.Radius = 2;
acBlkTblRec.AppendEntity(acCirc);
// Add an attribute definition to the block using (AttributeDefinition acAttDef = new AttributeDefinition()) { acAttDef.Position = new Point3d(0, 0, 0); acAttDef.Prompt = "Door #: "; acAttDef.Tag = "Door#"; acAttDef.TextString = "DXX"; acAttDef.Height = 1; acAttDef.Justify = AttachmentPoint.MiddleCenter; acBlkTblRec.AppendEntity(acAttDef);
// Create and insert the new block reference if (blkRecId != ObjectId.Null) { BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
using (BlockReference acBlkRef = new BlockReference(new Point3d(2, 2, 0), blkRecId)) { BlockTableRecord acCurSpaceBlkTblRec; acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
// Verify block table record has attribute definitions associated with it if (acBlkTblRec.HasAttributeDefinitions) { // Add attributes from the block table record foreach (ObjectId objID in acBlkTblRec) { DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;
if (dbObj is AttributeDefinition) { AttributeDefinition acAtt = dbObj as AttributeDefinition;
if (!acAtt.Constant) { using (AttributeReference acAttRef = new AttributeReference()) { acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform); acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
[CommandMethod("GettingAttributes")] publicvoidGettingAttributes() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if (!acBlkTbl.Has("TESTBLOCK")) { using (BlockTableRecord acBlkTblRec = new BlockTableRecord()) { acBlkTblRec.Name = "TESTBLOCK";
// Set the insertion point for the block acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add an attribute definition to the block using (AttributeDefinition acAttDef = new AttributeDefinition()) { acAttDef.Position = new Point3d(5, 5, 0); acAttDef.Prompt = "Attribute Prompt"; acAttDef.Tag = "AttributeTag"; acAttDef.TextString = "Attribute Value"; acAttDef.Height = 1; acAttDef.Justify = AttachmentPoint.MiddleCenter; acBlkTblRec.AppendEntity(acAttDef);
// Create and insert the new block reference if (blkRecId != ObjectId.Null) { BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
using (BlockReference acBlkRef = new BlockReference(new Point3d(5, 5, 0), acBlkTblRec.Id)) { BlockTableRecord acCurSpaceBlkTblRec; acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
// Verify block table record has attribute definitions associated with it if (acBlkTblRec.HasAttributeDefinitions) { // Add attributes from the block table record foreach (ObjectId objID in acBlkTblRec) { DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;
if (dbObj is AttributeDefinition) { AttributeDefinition acAtt = dbObj as AttributeDefinition;
if (!acAtt.Constant) { using (AttributeReference acAttRef = new AttributeReference()) { acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform); acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
[CommandMethod("AttachingExternalReference")] publicvoidAttachingExternalReference() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Create a reference to a DWG file string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg"; ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");
// If a valid reference is created then continue if (!acXrefId.IsNull) { // Attach the DWG reference to the current space Point3d insPt = new Point3d(1, 1, 0); using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId)) { BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
[CommandMethod("DetachingExternalReference")] publicvoidDetachingExternalReference() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Create a reference to a DWG file string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg"; ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");
// If a valid reference is created then continue if (!acXrefId.IsNull) { // Attach the DWG reference to the current space Point3d insPt = new Point3d(1, 1, 0); using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId)) { BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
[CommandMethod("ReloadingExternalReference")] publicvoidReloadingExternalReference() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Create a reference to a DWG file string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg"; ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");
// If a valid reference is created then continue if (!acXrefId.IsNull) { // Attach the DWG reference to the current space Point3d insPt = new Point3d(1, 1, 0); using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId)) { BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
[CommandMethod("UnloadingExternalReference")] publicvoidUnloadingExternalReference() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Create a reference to a DWG file string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg"; ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");
// If a valid reference is created then continue if (!acXrefId.IsNull) { // Attach the DWG reference to the current space Point3d insPt = new Point3d(1, 1, 0); using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId)) { BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
[CommandMethod("BindingExternalReference")] publicvoidBindingExternalReference() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Create a reference to a DWG file string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg"; ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");
// If a valid reference is created then continue if (!acXrefId.IsNull) { // Attach the DWG reference to the current space Point3d insPt = new Point3d(1, 1, 0); using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId)) { BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
[CommandMethod("ClippingExternalReference")] publicvoidClippingExternalReference() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Create a reference to a DWG file string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg"; ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");
// If a valid reference is created then continue if (!acXrefId.IsNull) { // Attach the DWG reference to the current space Point3d insPt = new Point3d(1, 1, 0); using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId)) { BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
Application.ShowAlertDialog("The external reference is attached.");
Matrix3d mat = acBlkRef.BlockTransform; mat.Inverse();
Point2dCollection ptCol = new Point2dCollection();
// Define the first corner of the clipping boundary Point3d pt3d = new Point3d(-330, 400, 0); pt3d.TransformBy(mat); ptCol.Add(new Point2d(pt3d.X, pt3d.Y));
// Define the second corner of the clipping boundary pt3d = new Point3d(1320, 1120, 0); pt3d.TransformBy(mat); ptCol.Add(new Point2d(pt3d.X, pt3d.Y));
// Define the normal and elevation for the clipping boundary Vector3d normal; double elev = 0;
if (acCurDb.TileMode == true) { normal = acCurDb.Ucsxdir.CrossProduct(acCurDb.Ucsydir); elev = acCurDb.Elevation; } else { normal = acCurDb.Pucsxdir.CrossProduct(acCurDb.Pucsydir); elev = acCurDb.Pelevation; }
// Set the clipping boundary and enable it using (Autodesk.AutoCAD.DatabaseServices.Filters.SpatialFilter filter = new Autodesk.AutoCAD.DatabaseServices.Filters.SpatialFilter()) { Autodesk.AutoCAD.DatabaseServices.Filters.SpatialFilterDefinition filterDef = new Autodesk.AutoCAD.DatabaseServices.Filters.SpatialFilterDefinition(ptCol, normal, elev, 0, 0, true); filter.Definition = filterDef;
// Define the name of the extension dictionary and entry name string dictName = "ACAD_FILTER"; string spName = "SPATIAL";
// Check to see if the Extension Dictionary exists, if not create it if (acBlkRef.ExtensionDictionary.IsNull) { acBlkRef.CreateExtensionDictionary(); }
// Open the Extension Dictionary for write DBDictionary extDict = acTrans.GetObject(acBlkRef.ExtensionDictionary, OpenMode.ForWrite) as DBDictionary;
// Check to see if the dictionary for clipped boundaries exists, // and add the spatial filter to the dictionary if (extDict.Contains(dictName)) { DBDictionary filterDict = acTrans.GetObject(extDict.GetAt(dictName), OpenMode.ForWrite) as DBDictionary;
if (filterDict.Contains(spName)) { filterDict.Remove(spName); }
filterDict.SetAt(spName, filter); } else { using (DBDictionary filterDict = new DBDictionary()) { extDict.SetAt(dictName, filterDict);
[CommandMethod("AttachXDataToSelectionSetObjects")] publicvoidAttachXDataToSelectionSetObjects() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
string appName = "MY_APP"; string xdataStr = "This is some xdata";
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Request objects to be selected in the drawing area PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();
// If the prompt status is OK, objects were selected if (acSSPrompt.Status == PromptStatus.OK) { // Open the Registered Applications table for read RegAppTable acRegAppTbl; acRegAppTbl = acTrans.GetObject(acCurDb.RegAppTableId, OpenMode.ForRead) as RegAppTable;
// Check to see if the Registered Applications table record for the custom app exists if (acRegAppTbl.Has(appName) == false) { using (RegAppTableRecord acRegAppTblRec = new RegAppTableRecord()) { acRegAppTblRec.Name = appName;
// Define the Xdata to add to each selected object using (ResultBuffer rb = new ResultBuffer()) { rb.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName)); rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, xdataStr));
SelectionSet acSSet = acSSPrompt.Value;
// Step through the objects in the selection set foreach (SelectedObject acSSObj in acSSet) { // Open the selected object for write Entity acEnt = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite) as Entity;
// Append the extended data to each object acEnt.XData = rb; } } }
// Save the new object to the database acTrans.Commit();