CubeCuttingOut.js Parameter Definition

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

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
wildspidee
Full Member
Posts: 84
Joined: Sat Nov 03, 2012 2:00 am

CubeCuttingOut.js Parameter Definition

Post by wildspidee » Wed Jan 21, 2015 12:02 am

I am using the code from ExText.js as a starting point, as it is very simple and easy to understand. I pulled everything out of the ExText.prototype.beginEvent (except the DrawExamples top line) and put it into another function and tried to call it. This didn’t work because of two related issues, the this.getDocument() in the new RTextEntity and the this.getDocumentInterface() at the end prior to terminate. Obviously, the prototype isn’t available in my new function.

I then referred to the CubeCuttingOut.js, which is a good example of the ability to create various functions that can be called as needed. Throughout the file, the parameter documentInterface is passed into nearly every function. However, I can’t find anywhere that this parameter has been defined. I looked in the included library.js and the CubeCuttingOut.js.

Can you provide the code necessary to define a variable, such as documentInterface or explain where it is defined in the CubeCuttingOut.js?

Thank you.
Last edited by wildspidee on Wed Jan 21, 2015 5:03 pm, edited 1 time in total.

wildspidee
Full Member
Posts: 84
Joined: Sat Nov 03, 2012 2:00 am

Re: CubeCuttingOut.js Parameter Definition

Post by wildspidee » Wed Jan 21, 2015 12:09 am

This works, but I wouldn't think this is correct long term -

Code: Select all

FMBodice.prototype.beginEvent = function() {
    FMScripts.prototype.beginEvent.call(this);
    FMBodice.fmtext(this.getDocument(), this.getDocumentInterface());


};

FMBodice.fmtext = function(gDocument, gDocumentInterface) {
    var pos = new RVector(5,10)

    // create an operation for adding objects:
    var op = new RAddObjectsOperation();
    // create a text entity and add it to the operation:
    var text = new RTextEntity(
        gDocument,
        new RTextData(
            pos,                 // position
            pos,                 // alignment point
            2.0,                 // height
            2.0,                 // text width (ignored for now)
            RS.VAlignTop,        // alignments
            RS.HAlignCenter,
            RS.LeftToRight,
            RS.Exact,
            1.0,                 // line spacing factor
            "New Test",              // the text
            "Arial",      // font
            false,        // bold
            false,        // italic
            0.0,          // angle
            false         // simple text without formatting
        )
    );
    op.addObject(text);

    //qDebug();
    // apply the operation to the current drawing:
    gDocumentInterface.applyOperation(op);
}

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

Re: CubeCuttingOut.js Parameter Definition

Post by andrew » Wed Jan 21, 2015 11:24 am

wildspidee wrote:Can you provide the code necessary to define a variable, such as documentInterface or explain where it is defined in the CubeCuttingOut.js?
QCAD calls these functions to generate the item or the item preview. You can simply rely on the parameter being passed to MyItem.generate and MyItem.generatePreview.

In other words
MyItem.generate = function(documentInterface) {
    ...
};
defines a function which is called by QCAD to generate the item's drawing entities when the item is inserted. documentInterface is a variable to the documentInterface of the document into which the item is being inserted. You need documentInterface to add the entities you want to insert when the user inserts your script item from the library browser.

wildspidee
Full Member
Posts: 84
Joined: Sat Nov 03, 2012 2:00 am

Re: CubeCuttingOut.js Parameter Definition

Post by wildspidee » Wed Jan 21, 2015 4:14 pm

Since CubeCuttingOut is an explicit example of a library part, this code is perfect for that purpose. Since eventually all my patterns will be parts, I'll switch over to this when the time comes.

I appreciate the great explanation.

Lori

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”