Page 1 of 2

Script language questions

Posted: Mon Dec 26, 2011 7:15 pm
by alf2006x
Hello.

How can I get the self script path in my script? For example, if I want to move my result file in directory with script file.

And how can I get in script the path of qcad directory?

Re: Script language questions

Posted: Mon Dec 26, 2011 7:29 pm
by andrew
alf2006x wrote:How can I get the self script path in my script? For example, if I want to move my result file in directory with script file.
The global variable 'includeBasePath' is set when your script is being included. If you need that information later, please store it when your script is being included, as it will be overwritten for each newly included file.

Example:

Code: Select all

function MyClass() {
    ...
}

MyClass.prototype = new MyBaseClass();

// store include base path for later use:
MyClass.includeBasePath = includeBasePath;

MyClass.prototype.beginEvent = function() {
    this.loadSomeResource(MyClass.includeBasePath + "/MyResource.txt");
};

...
And how can I get in script the path of qcad directory?

Code: Select all

QCoreApplication.applicationDirPath()
This might do the trick for you.

For details, please refer to:
http://developer.qt.nokia.com/doc/qt-4. ... ionDirPath

Posted: Mon Dec 26, 2011 7:57 pm
by alf2006x
Thanks a lot.
That works great.

Posted: Tue Jan 03, 2012 9:50 am
by alf2006x
How can I insert my block from the file?
I have got the file with name "MyDraw.dxf". It has block with name "MyBlock1". I want my script to open that file, get that block and insert it in current drawing.

Posted: Sun Jan 08, 2012 10:31 pm
by hungerburg
alf2006x wrote:How can I insert my block from the file?
I have got the file with name "MyDraw.dxf". It has block with name "MyBlock1". I want my script to open that file, get that block and insert it in current drawing.
I wrapped block pasting like below. I am sure you can dig into the loaded document and select a block. I just paste the file as is, and it will become a block in the target.

Code: Select all

/** Einen Block einfügen, gegebenenfalls regional spezifisch.
 * @param {Object} zeichnung die Zeichnung
 * @param {String} pfad Name der Datei
 * @param {Number} x X-Koordinate
 * @param {Number} y Y-Koordinate
 * @param {Boolean} h H-Spiegelung
 * @param {Boolean} v V-Spiegelung
*/
MyScript.prototype.insert = function(zeichnung, pfad, x, y, h, v) {

	var bPath = "My/" + pfad + ".dxf";
	var bName = "My#" + pfad;

	var bDoc = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
	var bInt = new RDocumentInterface(bDoc);
	if (bInt.importFile(bPath) != RDocumentInterface.IoErrorNoError) {
		this.log.w("Datei fehlt: " + bPath);
		return;
	}

	var operation = new RPasteOperation(bDoc);
	operation.setOffset(new RVector(x, y));
	operation.setBlockName(bName);
	operation.setScale(1.0);
	operation.setRotation(0.0);
	operation.setFlipHorizontal(h);
	operation.setFlipVertical(v);
	operation.setToCurrentLayer(true);
	operation.setOverwriteLayers(false);
	operation.setOverwriteBlocks(false);

	zeichnung.documentInterface.applyOperation(operation);
	zeichnung.documentInterface.repaintViews();
}

Posted: Mon Jan 09, 2012 6:31 am
by alf2006x
Thank you very much, hungerburg!
And there is one more thing I still can't understand. Can I use in my -no-gui QCAD script any commands without a prefix "R", like Circle3P
If so, can you write an example of using this commands?

Posted: Tue Jan 10, 2012 12:02 am
by hungerburg
alf2006x wrote:Can I use in my -no-gui QCAD script any commands without a prefix "R", like Circle3P?
I would study the source of Circle3P command. How it converts the data gathered from the mouse clicks into something, that the R… function can process. Circle3P is a funny tool, maybe a tough nut to crack with just a geometry book - please share your function, when its ready.

Posted: Tue Jan 10, 2012 6:39 am
by alf2006x
I would study the source of Circle3P command. How it converts the data gathered from the mouse clicks into something
I don't need to collect any input data from user (mouse clicks etc.) because my script works in NO-GUI mode. I just need to use command Circle3P with three params RPoint. Is it possible?

Posted: Tue Jan 10, 2012 9:13 pm
by andrew
Have a look at ShapeAlgorithms.createCircleFromThreePoints

You can find an example in Circle3P.js, line 146.

Posted: Wed Jan 11, 2012 9:41 am
by alf2006x
is there any easy and simple way to make this -no-gui script work without overwriting every command with input parameter 'guiAction', like Circle3P?

Code: Select all

var p1 = new RVector(0, 0);
var p2 = new RVector(10, 0);
var p3 = new RVector(5, Math.sin(RMath.deg2rad(60))*10);

operation.addObject(new RCircleEntity(document, new RCircleData(Circle3P(p1, p2, p3)));

Posted: Wed Jan 11, 2012 10:49 am
by andrew
The GUI action tools (Circle3P, Line2P, Trim, OpenFile, etc...) are interactive tools. These classes handle all user input to "do something".

Most of these tools rely on the QCAD Application Framework underneath to add an entity or an object to a drawing or modify an entity, etc.

In the same way, your script can use the functions in the QCAD Application Framework or static functions in the available scripts to achieve your goal.

Posted: Wed Jan 11, 2012 11:50 am
by alf2006x
yes,but in this case i must write my own function absolutely the same as qcad functions with minor changes.
it would be very comfortably for me to use qcad interactive functions with my variable params. if this is impossible i will just copy needed functions in my script and change their input parameters. i can't see other solution because my constructing of drawing use the objects, added earlier

Posted: Wed Jan 11, 2012 12:06 pm
by alf2006x
for example, on the top view i have to draw one part of construction first, and then draw the second part with clear rectangular or circle area before drawing second part. and number of parts of construction every time depends on user input data. it's really necessary instruments for me.

Posted: Sun Jan 15, 2012 1:54 pm
by alf2006x
Hello.
I almost understand the structure of QCAD Application Framework. That is really powerful instrument of 2D drawing.

But I have one more question.
I try to execute the script below. It rounds two lines, but do not erase original lines. I thought if I make parameter Prewiew=false it will erase original lines, but it not works.
Do I must erasing original lines myself in my script or there is some solution to make it under Round function?

Code: Select all

include(QCoreApplication.applicationDirPath()+"/scripts/Modify/Round/Round.js");

var p1 = new RVector(0, 0);
var p2 = new RVector(10, 0);
var p3 = new RVector(5, Math.sin(RMath.deg2rad(60))*10);

var LineEntity1 = new RLineEntity(document, new RLineData(p1, p2));
var LineEntity2 = new RLineEntity(document, new RLineData(p2, p3));

operation.addObject(LineEntity1);
operation.addObject(LineEntity2);

var success = Round.round(operation,LineEntity1,p1,LineEntity2,p3,true,1.5,false);
[/code]

Posted: Sun Jan 15, 2012 2:45 pm
by andrew
The round tool rounds a corner that is formed by two entities and optionally trims the two original entities to the rounding.

It does not have an option to remove the two original entities completely, so you will have to do that in a separate operation if desired.