Detection of changes made in Autocad/Civil 3D with .NET

Detection of changes made in Autocad/Civil 3D with .NET

cengizincebi
Contributor Contributor
756 Views
4 Replies
Message 1 of 5

Detection of changes made in Autocad/Civil 3D with .NET

cengizincebi
Contributor
Contributor

Hi everyone, 

I want it to detect and run when I add/remove a point group to Civil3d. 

 

But my code only recognizes the groups it sees at startup (while the form is loading) and detects it while working.

It does not recognize groups added/deleted later.

 

Can you help me? 

 

 

Public Class FRM_SampleLineExport
    Dim oPG As Autodesk.Civil.DatabaseServices.PointGroup = Nothing
 
    Private Sub frm_Load()
        Dim trans As Transaction = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()
        Dim civilDoc As CivilDocument = CivilApplication.ActiveDocument
        Dim PointGroupIDs As Autodesk.Civil.DatabaseServices.PointGroupCollection
        PointGroupIDs = civilDoc.PointGroups
 
        For Each PointGroupID In PointGroupIDs
            oPG = PointGroupID.GetObject(OpenMode.ForRead)
            If oPG.Id.ToString = "" Then
                Exit For
            End If
            Column7.Items.Add(oPG.Name.ToString)
            AddHandler oPG.Modified, AddressOf ChangePGs
        Next
    End Sub
 
    Public Sub ChangePGs(ByVal senderObj As Object, ByVal evtArgs As EventArgs)
             ' When a new point group is added to the file
            ' When group is deleted from file
    End Sub
End Class
0 Likes
757 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor

You may want to provide more description on the situation when/why you need to know a point group (or any object, for that matter) is added, modified or deleted. What role the form (your custom UI, obviously) plays in this situation (how the UI is displayed make HUGE difference here: being modeless or modal!), because it seems only the UI need to show the point group information and the potential changes.

 

If you show the UI as modal (i.e. a Dialog box), then point groups in the drawing CANNOT BE CHANGED/ADDED/DELETED, UNELSS the action is triggered by the UI itself (say, user clicks a button to add/change/delete), therefore, your code knows when the changes occur.

 

If the UI is modeless, then things are quite complicated. You need to handle various events (Entity.ObjectModified, or Database.ObjectAppended/Modified/Erased), or you may try to use ObjectOverrule to monitor the possible changes and decide a proper way to do (or not to do) something once changes are detected. This monitoring/handling work should have nothing to do with your modeless UI, but you probably need to decide whether the UI needs a refresh or not. As I said, it is rather complicated/advanced AutoCAD programming task. Not sure what your skill level is, I just stop here.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

cengizincebi
Contributor
Contributor

Hi,

 

Norman, thank you very much for your information and lead.

My form modeless :(, I had a manually operated button that did this job.

I don't like modal UI (unless you have to-not friendly to me)

 

I wanted to make something user friendly and automatic.

I just didn't want to redirect the user to the button all the time.

I understand from your notes (I examined the methods) that things are very complicated,

and to avoid wasting any more time I will continue with the button 🙂

 

But if anyone wants to share the solution code 😀 ,

     I wouldn't say no, if I find it, I'll publish it too 😉

 

 

 

0 Likes
Message 4 of 5

norman.yuan
Mentor
Mentor

Based on what you described, I assume you want to have a modeless form/PaletteSet to show/monitor current status of PointGroups in drawing. That is, when a PointGroup is added, existing one is modified (name change, for example) or erased; or even the CogoPoints in a group is changed, you want to form view updated automatically. The UI would behave just like Properties window, but only show CogoPoint/PointGroup information. It, of course, can be done with Acad/C3d .NET API, but it is a quite involving task. 

 

To do this type task, it would be very important to structure your solution properly to separate the functionality of the operation. Here is roughly I'd do:

 

1. Create a functional component (one or more classes) that handle the change tracking work

  • add handlers to Database.ObjectAppended/ObjectModified/ObjectErased events
  • Inside the event handlers, examine the e.DBObject is PointGroup/CogoPoint or not. If yes, save the ObjectId of them into a corresponding collections
  • When the first affected ObjectId is collected, add handler to Application.Idle event (you probably want to set a flag indicating this idle event handler is set because of affected ObjectId has been collected and waiting for current operation, likely a executing command, to be finished)
  • Once idle event handler runs, you examine the collected ObjectIds to see which CogoPoint/PointGroup added/modified/erase and organize the updated information into a data model. Then you move the idle event handler
  • Once the updated information data model is ready, raise a custom event with the data model as event argument

2. Build the modeless UI. It would much better to use WPF in MVVM pattern, so, that you can have the ViewModel to subscribe the aforementioned custom event. that is, whenever the tracking component raises its "change" event, the ViewModel would use the data model passed through the event argument to update itself, thus the UI. 

 

As you can see, you at least structure the code into 2 separate parts/components (change tracking and change presenting) and the 2 would only loosely couple together with event subscription and the common knowledge for both side is the updated information data model. Thus code modification on either side would have no, or minimum impact to the other.

 

If you wan to tracking other C3D objects, such as Parcel/Network... things could get event more complicated. For example, ParcelSegment change (adding/modifying/erasing) would not limited to itself, it would also affect one or more Parcels (being added/modified/deleted).

 

Not sure my description make how much sense to you or not. If not much, you may still have a lot to study up to the task, I am afraid.

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 5

cengizincebi
Contributor
Contributor

Hi Norman,
I understood your descriptions.

Because, you have summarized it beautifully in a simple and understandable manner 👍.

Thank you very much.

 

I'm an amateur software developer somewhere above entry level and below intermediate level. I am a survey engineer, but I'm interested in coding

 

In fact, I was going to do a study on the parcels in the future.
I decided to suspend it for a while 🙂
I will continue the point groups with the refresh button.

 

I will investigate Database.ObjectAppished / ObjectModified / ObjectErased events and examine their usage patterns. After solving-understanding-learning these, I will go back and revise the form.

 

Also, if I can, I will share it here.

0 Likes