Message 1 of 2
I'm trying to write code for block replacement in C#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm new to C# and AutoCAD plug-in development.
I wrote the code to replace the block, but I was able to confirm that SourceBlock does not have Attribute.
I think there is something missing or wrong in this code, can you help me?
What I want to do with this custom command is:
- When you start the custom command, you will be prompted to click the replacement source symbol. Click the replacement source symbol.
- Click the symbol to replace when prompted to click the symbol to replace (you can also select multiple symbols with range selection)
- Symbols are replacemant
I want to do something like this with this code.
I will write the code below.
Waiting for your reply.
thank you
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace BlockReplace
{
public class Class1
{
[CommandMethod("BlockReplace")]
public static void BolockReplace()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
// Select block to replace
PromptEntityOptions peo1 = new PromptEntityOptions("\nSelect source block:");
peo1.SetRejectMessage("\nPlease select a block.");
peo1.AddAllowedClass(typeof(BlockReference), true);
PromptEntityResult per1 = ed.GetEntity(peo1);
if (per1.Status == PromptStatus.OK)
{
ed.WriteMessage("Entity selected: " + per1.ObjectId.ToString());
}
else
{
ed.WriteMessage("No entity selected.");
}
if (per1.Status != PromptStatus.OK || per1.ObjectId.IsNull || per1.ObjectId.IsErased)
{
ed.WriteMessage("\nThe selected object is invalid or erased.");
return;
}
BlockReference sourceBlock = per1.ObjectId.GetObject(OpenMode.ForRead) as BlockReference;
if (sourceBlock == null)
{
ed.WriteMessage("\nError: Could not get source block.");
return;
}
// Select block to replace
PromptEntityOptions peo2 = new PromptEntityOptions("\nSelect target block:");
peo2.SetRejectMessage("\nPlease select a block.");
peo2.AddAllowedClass(typeof(BlockReference), true);
PromptEntityResult per2 = ed.GetEntity(peo2);
if (per2.Status == PromptStatus.OK)
{
ed.WriteMessage("Entity selected: " + per2.ObjectId.ToString());
}
else
{
ed.WriteMessage("No entity selected.");
}
if (per1.Status != PromptStatus.OK || per2.ObjectId.IsNull || per2.ObjectId.IsErased)
{
ed.WriteMessage("\nThe selected object is invalid or erased.");
return;
}
BlockReference targetBlock = per2.ObjectId.GetObject(OpenMode.ForRead) as BlockReference;
if (targetBlock == null)
{
ed.WriteMessage("\nError: Could not get target block.");
return;
}
if(sourceBlock.AttributeCollection.Count == 0)
{
ed.WriteMessage("\nError: Source block does not have any attributes.");
return;
}
// Replacement
BlockTable bt = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
foreach (ObjectId id in sourceBlock.AttributeCollection)
{
AttributeReference ar = tr.GetObject(id, OpenMode.ForRead) as AttributeReference;
if (ar == null)
{
ed.WriteMessage("\nError: Could not get attribute reference.");
return;
}
BlockReference newBlock = new BlockReference(targetBlock.Position, targetBlock.BlockTableRecord);
newBlock.Rotation = targetBlock.Rotation;
newBlock.ScaleFactors = targetBlock.ScaleFactors;
foreach (ObjectId attDefId in newBlock.AttributeCollection)
{
AttributeReference newAttRef = tr.GetObject(attDefId, OpenMode.ForRead) as AttributeReference;
if (newAttRef.Tag == ar.Tag)
{
newBlock.UpgradeOpen();
newBlock.Position = sourceBlock.Position;
newBlock.TransformBy(Matrix3d.Displacement(targetBlock.Position.GetVectorTo(sourceBlock.Position)));
newBlock.DowngradeOpen();
newAttRef.UpgradeOpen();
newAttRef.TextString = ar.TextString;
newAttRef.DowngradeOpen();
sourceBlock.Erase();
ed.WriteMessage("\nSymbol replaced.");
break;
}
}
}
tr.Commit();
}
}
}
}