Message 1 of 12
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi all,
I've just created an interface window in AutoCAD using C# in WPF with the MVVM format. However, I've encountered some issues.
For details:
In the window view, I've created two buttons (SnapOnCommand and SnapOffCommand), which are functioning correctly. However, I would like to implement several other commands in AutoCAD without closing the window application.
Could anyone provide assistance with this matter?
// code in view
<Button Grid.Column="0"
Grid.Row="6"
Content="snap on"
Command="{Binding SnapOnCommand}"
CommandParameter="{Binding ElementName=MainWindow}" />
<Button Grid.Column="0"
Grid.Row="7"
Content="snap off"
Command="{Binding SnapOffCommand}"
CommandParameter="{Binding ElementName=MainWindow}" />
// code in model
public class HCNModel : BaseViewModel
{
public void DuplicateAndExplode(out List<ObjectId> explodedObjectIds)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
explodedObjectIds = new List<ObjectId>();
PromptEntityResult per = ed.GetEntity("\nchoose entity: ");
if (per.Status != PromptStatus.OK)
return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Entity ent = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;
if (ent == null)
return;
Entity entCopy = ent.Clone() as Entity;
if (entCopy == null)
return;
DBObjectCollection explodedObjects = new DBObjectCollection();
entCopy.Explode(explodedObjects);
foreach (DBObject obj in explodedObjects)
{
Entity explodedEntity = obj as Entity;
if (explodedEntity != null)
{
ObjectId explodedObjectId = btr.AppendEntity(explodedEntity);
tr.AddNewlyCreatedDBObject(explodedEntity, true);
explodedObjectIds.Add(explodedObjectId);
}
}
tr.Commit();
}
}
public void DeleteExplodedObjects(List<ObjectId> explodedObjectIds)
{
if (explodedObjectIds == null || explodedObjectIds.Count == 0)
{
return;
}
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null)
{
return;
}
Database db = doc.Database;
using (var tr = doc.TransactionManager.StartTransaction())
{
foreach (var objectId in explodedObjectIds)
{
try
{
Entity ent = tr.GetObject(objectId, OpenMode.ForWrite) as Entity;
if (ent != null && !ent.IsErased)
{
if (ent.OwnerId != ObjectId.Null)
{
ent.Erase();
}
}
}
catch (System.Exception ex)
{
doc.Editor.WriteMessage($"\nerror: {ex.Message}");
}
}
tr.Commit();
}
}
}
}
// code in viewmode
public class HCNViewModel : BaseViewModel
{
private List<ObjectId> _duplicatedObjectIds = new List<ObjectId>();
private HCNModel _HCNModel;
public HCNModel HCNModel
{
get { return _HCNModel; }
set { _HCNModel = value; OnPropertyChanged(nameof(HCNModel)); }
}
public ICommand SnapOnCommand { get; set; }
public ICommand SnapOffCommand { get; set; }
public HCNViewModel()
{
HCNModel = new HCNModel();
SnapOnCommand = new RelayCommand<object>((p) => true, (p) =>
{
List<ObjectId> explodedObjectIds;
HCNModel.DuplicateAndExplode(out explodedObjectIds);
_duplicatedObjectIds.AddRange(explodedObjectIds);
});
SnapOffCommand = new RelayCommand<object>((p) => true, (p) =>
{
Document doc = Application.DocumentManager.MdiActiveDocument;
List<ObjectId> explodedObjectIdsToRemove = new List<ObjectId>();
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
foreach (var objectId in _duplicatedObjectIds)
{
Entity ent = tr.GetObject(objectId, OpenMode.ForRead) as Entity;
if (ent != null && ent.OwnerId != ObjectId.Null)
{
explodedObjectIdsToRemove.Add(objectId);
}
}
tr.Commit();
}
HCNModel.DeleteExplodedObjects(explodedObjectIdsToRemove);
foreach (var objectIdToRemove in explodedObjectIdsToRemove)
{
_duplicatedObjectIds.Remove(objectIdToRemove);
}
});
}
}
Solved! Go to Solution.