Macro to solve snap problem (reset origins to (0,0))
Posted: Sat Feb 16, 2013 1:56 pm
Hi,
I posted some days ago my solution to a snap problem to entities (circles centers, etc.) inside blockReferencesEntities (see post viewtopic.php?f=33&t=2319).
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:
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.
I posted some days ago my solution to a snap problem to entities (circles centers, etc.) inside blockReferencesEntities (see post viewtopic.php?f=33&t=2319).
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);
di.applyOperation(operation);
// requery block after applying operation (seems to be necessary...)
block = document.queryBlock(blockID);
block.setName(name);
block.setOrigin(new RVector(0,0));
// change name (back to original name): expedient for forcing origin change
operation = new RModifyObjectOperation(block, false);
di.applyOperation(operation);
var blockEntsID = document.queryBlockEntities(blockID);
var subent;
var moveVect = new RVector (- oldOrigin.getX(), - oldOrigin.getY());
document.setCurrentBlock(blockID);
var op = new RAddObjectsOperation()
for (var i = 0; i < blockEntsID.length; i++) {
subEnt = document.queryEntity(blockEntsID[i]);
subEnt.move(moveVect);
op.addObject(subEnt);
}
op.apply(document, false);
}
document.setCurrentBlock("*Model_Space");
document.rebuildSpatialIndex();
di.clearPreview()
di.repaintViews();
}