Events Not Triggering After First Command in AutoCAD .NET API

Events Not Triggering After First Command in AutoCAD .NET API

Melis_YagmurQSH4P
Participant Participant
425 Views
4 Replies
Message 1 of 5

Events Not Triggering After First Command in AutoCAD .NET API

Melis_YagmurQSH4P
Participant
Participant

Hi everyone,

I’m using the AutoCAD .NET API to update block attributes when a block is rotated. The issue is that after the first rotation command, the CommandStarted and CommandEnded events do not trigger again for subsequent rotations.

I’ve attached event handlers to detect the ROTATE command, but they only fire for the first rotation. 

Has anyone encountered a similar issue or found a way to ensure events fire after every rotation?

Thanks!

0 Likes
426 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant

Hi,

If you don't post your code, you won't get any answers, only guesses.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

Keith.Brown
Advisor
Advisor

In my experience when the event handlers for autocad stop working it is almost always because there was an exception in my code that was not caught and handled correctly.

0 Likes
Message 4 of 5

Melis_YagmurQSH4P
Participant
Participant

public void Initialize()
{
Application.DocumentManager.MdiActiveDocument.CommandEnded += OnCommandEnded;
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nRotation Sync Loaded.");
}

public void Terminate()
{
Application.DocumentManager.MdiActiveDocument.CommandEnded -= OnCommandEnded;
}

private void OnCommandEnded(object sender, CommandEventArgs e)
{
if (e.GlobalCommandName.Equals("ROTATE", StringComparison.OrdinalIgnoreCase))
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

TypedValue[] filter = { new TypedValue((int)DxfCode.Start, "INSERT") };
SelectionFilter selectionFilter = new SelectionFilter(filter);
PromptSelectionResult selRes = ed.SelectAll(selectionFilter);

if (selRes.Status == PromptStatus.OK)
{
SelectionSet sset = selRes.Value;
var filteredBlocks = sset.Cast<SelectedObject>()
.Select(selObj => tr.GetObject(selObj.ObjectId, OpenMode.ForRead) as BlockReference)
.Where(blkRef => blkRef != null &&
blkRef.Name == "profile")
.ToList();

foreach (var blkRef in filteredBlocks)
{
if (blkRef != null)
{
UpdateAngleAttribute(tr, blkRef);
}
}
}

tr.Commit();
}
}
}

 

 

Here is my code and how I solved it. I was subscribing to CommandWillStart event too,

but when I delete is it worked only with OnCommandEnded event. I don't know if this is a logical solution or not but it works.

0 Likes
Message 5 of 5

ActivistInvestor
Mentor
Mentor

While you may think that code works, it only reacts to the Rotate command, and because it doesn't know what objects were rotated, it forces you to find all of your objects-of-interest. Another issue is that you're using Transactions inside an event handler, which is a no-no. Doing that will corrupt UNDO/REDO, which you probably haven't tested. You should be using an OpenCloseTransaction.

 

In the clip below, regardless of how the blocks are rotated, or with what command, the attributes always maintain a rotation of 0 relative to the X-axis.

 

acad_4IeSDqmeOU.gif

 

The code that does what you see in the clip can be found in this repository folder.