using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Windows.Forms;
public class InsertExternalBlock
{
[CommandMethod("InsertExtBlock")]
public void InsertBlockFromExternalFile()
{
// Get the current document and editor
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
// Prompt user to select a DWG file to insert as a block
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "DWG Files (*.dwg)|*.dwg",
Title = "Select the DWG File to Insert as a Block"
};
if (openFileDialog.ShowDialog() != DialogResult.OK)
{
ed.WriteMessage("\nOperation cancelled.");
return;
}
string dwgFilePath = openFileDialog.FileName;
// Run the AutoCAD INSERT command to insert the DWG as a block
ed.Command("_.-INSERT", dwgFilePath, "0,0,0", "1", "1", "0");
}
}
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
[assembly: CommandClass(typeof(YourNamespace.UpdateBlockAttributesCommand))]
namespace YourNamespace
{
public class UpdateBlockAttributesCommand : IExtensionApplication
{
public void Initialize() { }
public void Terminate() { }
[CommandMethod("UpdateBlockAttributes")]
public void UpdateBlockAttributes()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// Prompt user to enter the string
PromptStringOptions promptStringOptions = new PromptStringOptions("\nEnter the string to insert into the block attributes: ");
PromptResult promptResult = ed.GetString(promptStringOptions);
if (promptResult.Status != PromptStatus.OK)
{
ed.WriteMessage("\nOperation canceled.");
return;
}
string userInput = promptResult.StringResult;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Iterate through all block references in the drawing
BlockTable blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
foreach (ObjectId blockId in blockTable)
{
BlockTableRecord blockRecord = (BlockTableRecord)tr.GetObject(blockId, OpenMode.ForRead);
// Check if block name contains "Attrib"
if (blockRecord.Name.Contains("Attrib"))
{
// Iterate through all block references in the current space (ModelSpace or PaperSpace)
BlockTableRecord currentSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (ObjectId entityId in currentSpace)
{
Entity entity = (Entity)tr.GetObject(entityId, OpenMode.ForRead);
if (entity is BlockReference blockRef)
{
// Check if the block reference matches the desired block
if (blockRef.Name.Equals(blockRecord.Name, StringComparison.OrdinalIgnoreCase))
{
foreach (ObjectId attId in blockRef.AttributeCollection)
{
AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);
attRef.TextString = userInput;
}
}
}
}
}
}
tr.Commit();
}
ed.WriteMessage("\nAttributes updated for blocks with 'Attrib' in their name.");
}
}
}
using System;
using System.Text;
using System.Windows.Forms; // For MessageBox
using cad = Autodesk.AutoCAD.ApplicationServices.Application;
using App = Autodesk.AutoCAD.ApplicationServices;
using Db = Autodesk.AutoCAD.DatabaseServices;
using Ed = Autodesk.AutoCAD.EditorInput;
using Rtm = Autodesk.AutoCAD.Runtime;
using Comp = ACSMCOMPONENTS24Lib;
using Autodesk.Civil.DatabaseServices;
[assembly: Rtm.ExtensionApplication(typeof(Bushman.AutoCAD.SheetSetTools.SheetSetCommands))]
[assembly: Rtm.CommandClass(typeof(Bushman.AutoCAD.SheetSetTools.SheetSetCommands))]
namespace Bushman.AutoCAD.SheetSetTools
{
public class SheetSetCommands : Rtm.IExtensionApplication
{
const string ns = "bush"; // namespace
[Rtm.CommandMethod(ns, "ss-get-properties", Rtm.CommandFlags.Modal)]
public void GetSheetSetProperties()
{
App.Document doc = cad.DocumentManager.MdiActiveDocument;
Db.Database db = doc.Database;
Ed.Editor ed = doc.Editor;
Comp.AcSmSheetSetMgr mng = new Comp.AcSmSheetSetMgr();
Comp.IAcSmEnumDatabase enumerator = mng.GetDatabaseEnumerator();
enumerator.Reset();
Comp.AcSmDatabase smDb = enumerator.Next();
if (smDb != null)
{
try
{
smDb.LockDb(db);
Comp.AcSmSheetSet sheetset = smDb.GetSheetSet();
if (sheetset != null)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"Sheet Set Name: {sheetset.GetName()}");
sb.AppendLine($"Description: {sheetset.GetDesc()}");
// Retrieve custom properties
Comp.AcSmCustomPropertyBag customProperties = sheetset.GetCustomPropertyBag();
if (customProperties != null)
{
// List of desired custom property names
string[] desiredProperties = new string[]
{
"Project Name",
"Project Number",
"Issued Date:",
"Title1",
"Title2",
"Title3",
"TotSheets"
};
// Iterate through desired properties
foreach (var propertyName in desiredProperties)
{
try
{
var property = customProperties.GetProperty(propertyName) as Comp.AcSmCustomPropertyValue;
if (property != null)
{
sb.AppendLine($"{propertyName}: {property.GetValue()}");
}
else
{
sb.AppendLine($"{propertyName}: [Not Found]");
}
}
catch (Exception ex)
{
sb.AppendLine($"{propertyName}: Error retrieving value - {ex.Message}");
}
}
}
else
{
sb.AppendLine("No custom properties found.");
}
// Display the properties in a message box
MessageBox.Show(sb.ToString(), "Sheet Set Properties");
}
else
{
ed.WriteMessage("\nNo sheet set is available.");
}
}
catch (Exception ex)
{
ed.WriteMessage($"\nError: {ex.Message}");
}
finally
{
smDb.UnlockDb(db, true);
}
}
else
{
ed.WriteMessage("\nNo sheet set is open.");
}
}
private string GetCustomPropertyValue(Comp.AcSmCustomPropertyBag customProperties, string propertyName)
{
try
{
var property = customProperties.GetProperty(propertyName) as Comp.AcSmCustomPropertyValue;
if (property != null)
{
return property.GetValue();
}
}
catch (Exception ex)
{
// Log or handle the exception if needed
Console.WriteLine($"Error retrieving custom property {propertyName} - {ex.Message}");
}
return null;
}
public void Initialize()
{
App.Document doc = cad.DocumentManager.MdiActiveDocument;
Ed.Editor ed = doc.Editor;
ed.WriteMessage("\nSheetSetTools. © Andrey Bushman, 2013\n\n");
}
public void Terminate()
{
// Empty body.
}
}
}