Reacting to document modifications

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Reacting to document modifications

Post by Ghost_of_Magellan » Mon Aug 08, 2016 10:58 am

I'm stumped... so I made 3 tests that went to hell, but i required components of two of them to make it happen!

That's awesome.

Well, now I got it running. Thank you, andrew.

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Reacting to document modifications

Post by Ghost_of_Magellan » Mon Aug 08, 2016 11:11 am

Just to hammer it down for those who are searching for a code example:
// create a new transaction listener:
AutoRefresh = new RTransactionListenerAdapter();	//Considers every alteration
appWin.addTransactionListener(AutoRefresh);

//Will be called whenever there is a change in objects
AutoRefresh.transactionUpdated.connect(function(document, vl_changes) {
        if (isNull(document) || isNull(vl_changes)) {
            return;
        }
	
        vl_objIds = vl_changes.getAffectedObjects();	//Returns Ids of affected objects in array form

        vl_change_list = vl_changes.getPropertyChanges(vl_objIds);        //Returns the specific changes in array form
        for(i=0; i<vl_change_list.length; i++){
                test1 = vl_change_list.getNewValue();        //Gets the value into which the entity was changed
                test2 = vl_change_list.getOldValue();        //Gets the value from which the entity was changed
                test3 = vl_change_list.getPropertyTypeId();        //Gets the RPropertyTypeId variable where the Id and custom names are contained
                test4 = test3.getId();        //Gets the Id number of the alteration from the previous RPropertyTypeId variable.
        }
}


Adapt it to your needs.

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Reacting to document modifications

Post by Ghost_of_Magellan » Mon Aug 08, 2016 12:38 pm

Is there any list of the modification Id's that we can peruse?
It's kind of annoying to use the debugger to individually test for them =P

Thank you.

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

Re: Reacting to document modifications

Post by andrew » Mon Aug 08, 2016 1:00 pm

Note sure I understand. Please elaborate. getAffectedObjects() does return a list of IDs of entities that were modified.

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Reacting to document modifications

Post by Ghost_of_Magellan » Mon Aug 08, 2016 2:07 pm

getAffectedObjects() provides a list of entities that have been changed.
getPropertyChanges(vl_objIds) provides a list of changes on that particular entity, the Id of THE CHANGE is provided by: test3 = vl_change_list.getPropertyTypeId(); test4 = test3.getId();
Each type of change has an ID associated. 6 for line type, 7 for scale, 8 for line weight, 9 for color change, 30 for total lenght, 115 for start point X, etc.
My question pertains to this type of ID. A list what what Ids there are and what type of change they represent would be apreciated.

Thank you.

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

Re: Reacting to document modifications

Post by andrew » Mon Aug 08, 2016 4:20 pm

I'd strongly recommend to NEVER use these integer IDs in your code. Those IDs are generated at run time and can change in every future release and theoretically even every time the application is run.

To look up a particular modification or check if a change matches a change you are interested in, always compare to the appropriate property definition:
if (propertyChange.getPropertyTypeId().equals(RLineEntity.PropertyStartPointX)) {
    // x coordinate of the start point of a line has changed
}

if (propertyChange.getPropertyTypeId().equals(REntity.PropertyColor)) {
    // color of any entity has changed
}
You can find all properties listed in the sources for the respective entity class or in the API doc, e.g.:
RObject Properties available for all objects
REntity Properties available for all entities
RLineEntity Properties available for line entities
...

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Reacting to document modifications

Post by Ghost_of_Magellan » Mon Aug 08, 2016 5:36 pm

Your recomendation has been taken to heart, and the necessary adaptations have been done so my app doesn't implode when Qcad receives an update. Anyone who looks at my code now will probably be induced in error and think I actually know what I'm doing!

Thank you, andrew.

Ghost_of_Magellan
Full Member
Posts: 58
Joined: Wed Mar 16, 2016 5:10 pm

Re: Reacting to document modifications

Post by Ghost_of_Magellan » Tue Aug 09, 2016 9:50 am

I'm wondering if there is any way to temporarily cancel the listener. There are occasions where the app itself has to make changes to the entities and in such occasions it may not make sense to track these changes.
I reckon its this: removeTransactionListener()

So, I suppose I suppose I will have to add to the code that I don't want to track, the following:
appWin.removeTransactionListener(AutoRefresh);
//Code that changes entitties that must not be tracked.
appWin.addTransactionListener(AutoRefresh);
I tested this code and so far as I can determine, its working fine. Is there any recomendation against this method and, if there is, what is the alternative to it?

Thank you very much,

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”