Script language questions

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

Moderator: andrew

Script language questions

Postby alf2006x » Mon Dec 26, 2011 7:15 pm

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?
alf2006x
Active Member
 
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Re: Script language questions

Postby andrew » Mon Dec 26, 2011 7:29 pm

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
User avatar
andrew
Site Admin
 
Posts: 2658
Joined: Fri Mar 30, 2007 6:07 am

Postby alf2006x » Mon Dec 26, 2011 7:57 pm

Thanks a lot.
That works great.
alf2006x
Active Member
 
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Postby alf2006x » Tue Jan 03, 2012 9:50 am

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.
alf2006x
Active Member
 
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Postby hungerburg » Sun Jan 08, 2012 10:31 pm

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();
}
User avatar
hungerburg
Senior Member
 
Posts: 125
Joined: Fri May 28, 2010 7:35 pm

Postby alf2006x » Mon Jan 09, 2012 6:31 am

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?
alf2006x
Active Member
 
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Postby hungerburg » Tue Jan 10, 2012 12:02 am

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.
User avatar
hungerburg
Senior Member
 
Posts: 125
Joined: Fri May 28, 2010 7:35 pm

Postby alf2006x » Tue Jan 10, 2012 6:39 am

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?
alf2006x
Active Member
 
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Postby andrew » Tue Jan 10, 2012 9:13 pm

Have a look at ShapeAlgorithms.createCircleFromThreePoints

You can find an example in Circle3P.js, line 146.
User avatar
andrew
Site Admin
 
Posts: 2658
Joined: Fri Mar 30, 2007 6:07 am

Postby alf2006x » Wed Jan 11, 2012 9:41 am

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)));
alf2006x
Active Member
 
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Postby andrew » Wed Jan 11, 2012 10:49 am

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.
User avatar
andrew
Site Admin
 
Posts: 2658
Joined: Fri Mar 30, 2007 6:07 am

Postby alf2006x » Wed Jan 11, 2012 11:50 am

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
alf2006x
Active Member
 
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Postby alf2006x » Wed Jan 11, 2012 12:06 pm

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.
alf2006x
Active Member
 
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Postby alf2006x » Sun Jan 15, 2012 1:54 pm

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]
alf2006x
Active Member
 
Posts: 42
Joined: Mon Oct 10, 2011 11:29 am

Postby andrew » Sun Jan 15, 2012 2:45 pm

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.
User avatar
andrew
Site Admin
 
Posts: 2658
Joined: Fri Mar 30, 2007 6:07 am

Next

Return to QCAD 3 Developers and Contributors

Who is online

Users browsing this forum: No registered users and 0 guests