Script to create bounding boxes

Post here to ask questions about or get help with the scripting module of QCAD 2.1/2.2.

Moderator: andrew

Locked
denverh
Newbie Member
Posts: 5
Joined: Sat Dec 20, 2008 1:44 am

Script to create bounding boxes

Post by denverh » Sun Sep 11, 2011 5:20 pm

Hello,

Something I find myself doing fairly often is drawing boxes around objects. I've been doing this by placing vertical and horizontal lines at the four minimum/maximum points, then connecting them. However, it's not always easy to tell exactly where the minimum X, maximum X, minimum Y, and maximum Y points are. I thought a script that draws a bounding box around a selected entity would be a useful addition. But I don't see any way to access the necessary information. Even if I have to cycle through the coordinates of all the selected points, that would be fine. I just don't see a way to get any information about anything that's already selected. Is there a way to do that?

Regards,

Denver

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

Post by andrew » Mon Sep 12, 2011 7:42 pm

Scripting in QCAD 2 is limited to constructing simple entities without querying existing entities.

In the new ECMAScript interface of QCAD 3 (currently beta), this is easily possible.

More or less like this (from the top of my head, not tested):

Code: Select all

MyAction.beginEvent = function() {
  // get current document and document interface:
  var doc = this.getDocument();
  var di = this.getDocumentInterface();

  // get bounding box of current selection:
  var box = doc.getSelectionBox();

  // grow box if desired:
  box.grow(1.0);

  // convert box to lines:
  var lines = box.getLines2d();

  // add lines to document:
  var op = new RAddObjectsOperation();
  for (var i=0; i<lines.length; i++) {
    var entity = new RLineEntity(doc, new RLineData(lines[i]));
    op.addObject(entity);
  }
  di.applyOperation(op);
}

denverh
Newbie Member
Posts: 5
Joined: Sat Dec 20, 2008 1:44 am

Post by denverh » Thu Sep 29, 2011 11:36 pm

Thanks Andrew.

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

Post by andrew » Sun Nov 06, 2011 1:41 pm

denverh: QCAD 3 RC1 has an example script for that under scripts/Examples/DrawExamples/DrawBoundingBox

It's also available in the menu Examples - Drawing - Draw Bounding Box.

Usage:

1. Select entity(ies)
2. Start tool

A bounding box is drawn around each of the selected entities.

The script is thought as a starting point for more advanced, similar scripts.

Locked

Return to “QCAD 2.1/2.2 Developers”