Macro to solve snap problem (reset origins to (0,0))

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

Moderator: andrew

Junior Member
Posts: 19
Joined: Thu Nov 29, 2012 4:28 pm

Macro to solve snap problem (reset origins to (0,0))

Postby mariosboarina » Sat Feb 16, 2013 1:56 pm

I posted some days ago my solution to a snap problem to entities (circles centers, etc.) inside blockReferencesEntities (see post
The problem (IMO) was I had drawings with blocks origin different (or distant) from (0,0) and this (somehow) prevented QCad to snap correctly on centers, middle points, ends, etc. of entities from outside block referencies.
I wrote down some lines to reset blocks origin in drawing to (0,0) (and of course consequentially reposition entities inside those blocks).
I post them here in developers section, hoping it might be usefull.
This is it:

Code: Select all

MyFunctionsClass.purgeBlocksOrigin = function() {
   var di = EAction.getDocumentInterface();
   var document = di.getDocument();
   var allBlocksID = document.queryAllBlocks();
   var block, oldOrigin, name, operation, blockID;
   for (var k = 0; k < allBlocksID.length; k++) {
      blockID = allBlocksID[k]
      block =  document.queryBlock(blockID);
      oldOrigin = block.getOrigin();
      // var operation = new RAddObjectOperation(block, false);
      // di.applyOperation(operation);
      name = block.getName();
      // change name (necessary for later can do a change (back to original))
      block.setName(name + "_mod");
      operation = new RAddObjectOperation(block, false);
      // requery block after applying operation (seems to be necessary...)
      block =  document.queryBlock(blockID);
      block.setOrigin(new RVector(0,0));
      // change name (back to original name): expedient for forcing origin change
      operation = new RModifyObjectOperation(block, false);
      var blockEntsID = document.queryBlockEntities(blockID);
      var subent;
      var moveVect  = new RVector (- oldOrigin.getX(), - oldOrigin.getY());
      var op = new RAddObjectsOperation()
      for (var i  = 0; i < blockEntsID.length; i++) {
         subEnt = document.queryEntity(blockEntsID[i]);
      op.apply(document, false);

P.S.: I had problems to get setOrigin() working, and I kept the first solution that worked for me: that is changing block name also: this seems to force also setOrigin() to apply its changes (otherwise it didn't); in order to mantain the original name I also had to prevously change it to some other name (for changing it back to original being a real change). Not elegant at all, but it works, hope I'll find some better way.

User avatar
Moderator / Drawing Help
Posts: 1289
Joined: Thu Aug 25, 2011 9:28 pm
Location: UK

Re: Macro to solve snap problem (reset origins to (0,0))

Postby Clive » Mon Feb 18, 2013 12:01 pm

I'm sure the effort you've made here will come in handy for others - well done :wink:

Return to “QCAD Developers and Contributors”

Who is online

Users browsing this forum: No registered users and 4 guests