Message 1 of 13
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace Block_Replace
{
public class Replace_objects
{
[CommandMethod("ReplaceObjects")]
public void ReplaceObjects()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor editor = doc.Editor;
PromptSelectionResult selectionResult = editor.GetSelection();
if (selectionResult.Status != PromptStatus.OK)
{
editor.WriteMessage("No objects selected.");
return;
}
SelectionSet selectionSet = selectionResult.Value;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// Prompt user to select dynamic block reference
PromptEntityOptions promptOptions = new PromptEntityOptions("\nSelect dynamic block reference: ");
promptOptions.SetRejectMessage("Invalid selection. Please select a dynamic block reference.");
promptOptions.AddAllowedClass(typeof(BlockReference), true);
PromptEntityResult promptResult = editor.GetEntity(promptOptions);
if (promptResult.Status != PromptStatus.OK)
{
editor.WriteMessage("No dynamic block reference selected.");
return;
}
BlockReference dynamicBlockReference = trans.GetObject(promptResult.ObjectId, OpenMode.ForRead) as BlockReference;
foreach (ObjectId objectId in selectionSet.GetObjectIds())
{
Entity selectedEntity = trans.GetObject(objectId, OpenMode.ForRead) as Entity;
if (selectedEntity == null)
continue;
// Create a new block reference at the same position
BlockReference newBlockRef = dynamicBlockReference.Clone() as BlockReference;
newBlockRef.Position = selectedEntity.GeometricExtents.MinPoint;
// Add the new block reference to the model space
BlockTableRecord ms = trans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite) as BlockTableRecord;
ms.AppendEntity(newBlockRef);
trans.AddNewlyCreatedDBObject(newBlockRef, true);
}
trans.Commit();
}
}
}
}
ABOVE BLOCK NEED TO BE REPLACED BY OTHER BLOCK
THE BLOCK GOT PASTE BUT IT IS LOOSING ITS DYNMIC PROPERTIES I WANT TO RETAIN THOSE DAINAMIC PROPERTIES AFTER REPLACING ALSO.
I TRIED TO REPLACE THOSE BLOCKS BY THE ABOVE BLOCK
Solved! Go to Solution.