How to open a defined dwg file

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
gialanza
Newbie Member
Posts: 6
Joined: Mon Sep 13, 2021 7:36 am

How to open a defined dwg file

Post by gialanza » Sat Oct 16, 2021 9:02 am

I think is a newbie question for most of all but I'm very new to Qcad scripting.
I need to open a ( template ) dwg file a fill it with some texts and lines.
I don't understand how to use NewFile o OpenFile. I read the EXAMPLES but I only want to open a defined file.
Is there a simpler way to do this?

Thanks in advance for any help
Gianfranco

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to open a defined dwg file

Post by CVH » Sat Oct 16, 2021 3:06 pm

Hi,

First, one has to include the NewFile class high in your code with:

Code: Select all

include("scripts/File/NewFile/NewFile.js");
One should check upfront if the given filename exist.
As example:

Code: Select all

var fileName = " .... ";    // A full path and complete filename with suffix by any means
var fi = new QFileInfo(fileName); 

// When the file doesn't exists then issue a warning and return 'undefined':
if (!fi.exists()) {    // When the file doesn't exists
    appWin.handleUserWarning("File does not exists");
    return undefined;
}
Then we can open the file as a new tab in the GUI with:

Code: Select all

// Open the drawing file:
var w = NewFile.createMdiChild(fileName);    // Returns a mdiChild
Regards,
CVH
Last edited by CVH on Sun Oct 17, 2021 3:32 am, edited 2 times in total.

gialanza
Newbie Member
Posts: 6
Joined: Mon Sep 13, 2021 7:36 am

Re: How to open a defined dwg file

Post by gialanza » Sat Oct 16, 2021 7:45 pm

Hi CVH,
thanks again for your precious help ( and patience! ).

Gianfranco

mrhickman53
Junior Member
Posts: 17
Joined: Thu Sep 07, 2023 1:17 am

Re: How to open a defined dwg file

Post by mrhickman53 » Sat Sep 23, 2023 4:11 am

I have opened a file similarly to the script provided in this thread but cannot apply actions to the window. Even though the window is highlighted, all actions apply to the previous window. After opening w, I've added

Code: Select all

doc = w.getDocument();
di = w.getDocumentInterface();
What do I have to do to get actions to affect the new drawing?

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to open a defined dwg file

Post by CVH » Sat Sep 23, 2023 5:38 am

Hi,
mrhickman53 wrote:
Sat Sep 23, 2023 4:11 am
What do I have to do to get actions to affect the new drawing?
Your script actions probably run in the context of the former active drawing.

NewFile creates a new RMdiChildQt, you should be able to retrieve the document interface and document as you did:
https://qcad.org/doc/qcad/3.0/developer ... ld_qt.html

Code: Select all

var newDi = w.getDocumentInterface();
var newDoc = w.getDocument();
Then for example query entities from newDoc or send operations to the newDi.

Code: Select all

var ids = newDoc.querySelectedEntities();
...
var op = new RAddObjectsOperation();
...
newDi.applyOperation(op);

Search the forum for "setActiveSubWindow" for more info on switching between drawing tabs.

Regards,
CVH

mrhickman53
Junior Member
Posts: 17
Joined: Thu Sep 07, 2023 1:17 am

Re: How to open a defined dwg file

Post by mrhickman53 » Sat Sep 23, 2023 5:56 pm

There is still something else I need to do. What I have specifically done is modify the ImportFile.js script to import a file into a particular block in a specific, preloaded dxf. The new command is ImportAP. If I preload the templateAP.dxf (not a true template) with File-Open, then run the ImportAP script with

Code: Select all

var doc = this.getDocument();
var di = this.getDocumentInterface();
all works as intended. I am now trying to have ImportAP first load the templateAP.dxf before importing another file into it. The key modifications are

Code: Select all

ImportAP.prototype.beginEvent = function() {

    //*** doc and di could probably be this.doc, this.di
    //*** We are Importing to AP block


    var template = RSettings.getApplicationPath() + QDir.separator +
	"scripts/File/ImportAP/APtemplate.dxf";
    qDebug("template = ",template);
    var myChild = NewFile.createMdiChild(template);
    doc = myChild.getDocument();
    di = myChild.getDocumentInterface();

    // workaround for Qt keyboard focus bug:
    var appWin = RMainWindowQt.getMainWindow();
    if (!isNull(appWin)) {
        appWin.activateWindow();
        appWin.raise();
        appWin.setFocus(Qt.OtherFocusReason);
    }
    

    var mdiArea = EAction.getMdiArea();
    var windows = mdiArea.subWindowList();
    qDebug("#$#$ windows ", windows);
    mdiArea.setActiveSubWindow(windows[windows.length-1]);
    //mdiArea.setActiveSubWindow(windows[1]);

    //doc = this.getDocument();
    //di  = this.getDocumentInterface();
The attachment ImportAPQuestion1.png is no longer available
The templateAP.dxf file loads into a new subWindow, the subWindow is highlighted and the import dialog runs.
However, once a file is selected, it will only be pasted into the active block in the previous subWindow. I can select which subWindow is active before completing the paste but this does not alter the result.
ImportAPQuestion1.png
window before selecting file to be pasted
ImportAPQuestion1.png (183.75 KiB) Viewed 8657 times
Image demonstrating that the entity to be pasted is not previewed in the target subWindow

ImportAPQuestion2.png
Trying to paste into target subWindow
ImportAPQuestion2.png (123.19 KiB) Viewed 8657 times
A side-effect is that I can close only one of the two subWindows and cannot close the qcad application. I must kill it from a command line.
ImportAPQuestion3.png
can only paste into untitled subWindow
ImportAPQuestion3.png (136.53 KiB) Viewed 8657 times
ubuntu 22.04 qcad 3.28-1

PS. I am new to Javascript, Qt, and qcad scripting, so my use of jargon is highly questionable

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to open a defined dwg file

Post by CVH » Sat Sep 23, 2023 10:35 pm

mrhickman53,

Normally you should have started a new topic in this (sub-)forum.
Perhaps you can ask a moderator to move this conversation since Sat Sep 23, 2023 5:11 am to a new topic.

IMHO you are not pasting to your new document template.
Remind that importing is twofold, A) Opening a file in a background document B) Pasting the content to the destination document.

A) happens in ImportFile.js but remind this is a GUI tool.
https://github.com/qcad/qcad/tree/maste ... ImportFile
Line 31-33 create the background source document
Line 72 lets the sourceDI import the required drawing file.

B) happens in Paste.js, again a GUI tool.
https://github.com/qcad/qcad/blob/maste ... te.js#L129
You may have noticed that ImportFile.js is based on Paste and calls Paste for the user interaction.
Paste is further based on Edit and that on EAction.

In the beginEvent of Paste (Lines 55-71) the destination document interface this.di is set to this.getDocumentInterface();.
Meaning the current active drawing when you started your custom tool.
The actual pasting with preview is preformed in the pickCoordinate() of Paste.
On final placement (by the user) this will create an operation with getOperation().
The pasting operation is created in lines 129-151

I think it is easier to merge the functionality together.
- Create a newfile ... myChild
> Advice: Rename doc and di as destDoc and destDi
- Import a file to a background document ... sourceDocument and sourceDi
- Create a RPasteOperation
- Let destDi perform the operation
- Then swap the focus to the new drawing with the imported content.

Regards,
CVH

mrhickman53
Junior Member
Posts: 17
Joined: Thu Sep 07, 2023 1:17 am

Re: How to open a defined dwg file

Post by mrhickman53 » Sun Sep 24, 2023 1:21 am

Thanks for your response again. It will take me some time to digest the information you provided, though I have been reviewing Paste.js and Edit.js for hints as to how I can resolve my problem.

1. If I were to request this thread be moved to a new topic, do you have a suggestion on what topic that should be? I have been struggling to find a reasonable description of what I am trying to accomplish.

2. I have been using the gui-based examples as I have not identified any examples that implement background operations which have been helpful in educating me how to implement such. In the mean time, having the initial file and the pasted file in the foreground has been quite helpful for me to implement the various manipulations of the pasted block to my satisfaction.

3. My goal for loading the initial drawing as part of the paste operation is that I have full control of its loading rather than merely nagging that I cannot find it loaded. When I'm working myself, I actually use a bash script to load the initial file that I call (misleadingly) templateAP.dxf and run the ImportAP script as I start qcad, and that works fine. Rather than rely on others to use a similar shell script I would like to build the loading of templateAP.dxf into the ImportAP script itself. Mostly, it is coming down to stubborness because I got close to having it work really quickly and have not increased my knowledge of how it should work for several hours now. It is obvious that I do not know how the basic windowing system works and I am attempting to use this as a learning experience.

I did find a potential area to explore. Once I have read in the file with

Code: Select all

	
	var template = RSettings.getApplicationPath() + QDir.separator +
	    "scripts/File/ImportAP/APtemplate.dxf";
	qDebug("template = ",template);
	var myChild = NewFile.createMdiChild(template);
	this.doc = myChild.getDocument();
	this.di = myChild.getDocumentInterface();
This follows but I am not sure if it calls immediately or is creating a call method for the ImportAP.beginEvent(). I've got to learn more about protoype.

Code: Select all

    Paste.prototype.beginEvent.call(this);
where "this" is pointing to ImportAP instantiation of a Paste object who's document and document interface are probably the original untitled document. I need to pass an object that is of a type acceptible to this call that contains the document and document interface of the MdiChild created above. I only know that "myChild" is not the correct type even though it supports getDocument() and getDocumentInterface().

Again, thanks for your patience with a newbie.

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to open a defined dwg file

Post by CVH » Sun Sep 24, 2023 9:03 am

Hi,

1) It seems that you are trying to Import a drawing in a template on open by script, am I right?
2) There are other ways to use the standard GUI tools but as explained Paste uses the current active drawing to paste to.
That happens when you initiate your custom tool.
3) True it should function ... But you don't present the full code to verify.
I would be happy to help you if it were not a bunch of snippets. :wink:

ImportAP should first call the beginEvent of Paste.
Then ImportAP creates a new document from template and updates this.di as required.
Then it imports a file in this.sourceDi by file dialog.
When the beginEvent of ImportAP ends the tool is still in SettingPosition state initiated by Paste.
Before ImportAP beginEvent ends you need to set the GUI focus to the newly created tab.
The interaction on the newly created document is then preformed by Paste pickCoordinate.

Regards,
CVH

mrhickman53
Junior Member
Posts: 17
Joined: Thu Sep 07, 2023 1:17 am

Re: How to open a defined dwg file

Post by mrhickman53 » Sun Sep 24, 2023 5:06 pm

I have modified ImportFile to TestAP, which shows the same behavior as my ImportAP.

I have experimented with

Code: Select all

	var myChild = NewFile.createMdiChild(template);
	this.setDocumentInterface(myChild.getDocumentInterface());
which enables the pasting cross-hairs to appear in the active block of the loaded file, but the paste preview still does not show and the paste does not finish on the mouseclick.

My goal is to load templateAP and import into the AP or Ckt block, which is accomplished by making the appropriate block current prior to beginning the paste. The gory details are that a specific field in the filename established by its naming convention determines where the paste is made. This is working and not present in the files attached. Succesfully pasting into any block is sufficient for this test.

Again, thanks.
Attachments
TestAP.zip
ImportFile modified to first load templateAP.dxf then import block
(82.86 KiB) Downloaded 161 times

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to open a defined dwg file

Post by CVH » Sun Sep 24, 2023 6:15 pm

Had a quick look ...

TestAP.js .. Line 65-66:

Code: Select all

    this.doc = this.getDocument();
    this.di  = this.getDocumentInterface();
While Paste.js does the same a split second later ... Line 69:

Code: Select all

    this.di = this.getDocumentInterface();
The meaning of 'this' is here the current action.
The action you started from within another drawing document.
Every document has its own document interface.
Paste will thus act on that document that was active when you started the tool.

Import or TestAP for that matter enhances Paste with pasting 'asBlock' and a name.
The block name to use is handled in TestAP (= +/- Import) ... Line 181-191.
This results in a proposition based on the filename.
You just need to get the Options Toolbar child called 'BlockName' and set the text to whatever you desire.
slotBlockNameChanged will catch the change and update this.blockName.
this.blockName is then later used by Paste to preform the operation.

I have not a lot of time today left for trials.
I'll have a look at it tomorrow.

Just to be sure:
You start an new drawing from template and want to import a drawing in that as a block. Correct?

Regards,
CVH

mrhickman53
Junior Member
Posts: 17
Joined: Thu Sep 07, 2023 1:17 am

Re: How to open a defined dwg file

Post by mrhickman53 » Sun Sep 24, 2023 7:45 pm

Thank you for your help. I have taken a new approach which takes advantage of the NewFile.addPostOpenAction() method. By opening my template directly without engaging the ui for querying filenames and then calling my InsertAP as a postOpenAction things work as I intend. I'm still curious why I could not make the other method work, but figuring that out is now a low priority.

CVH
Premier Member
Posts: 3416
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to open a defined dwg file

Post by CVH » Tue Sep 26, 2023 4:34 am

mrhickman53 wrote:
Sun Sep 24, 2023 7:45 pm
I have taken a new approach which takes advantage of the NewFile.addPostOpenAction() method.
Probably the better solution. :wink:

The Paste action is already running in the document interface of your initial drawing.
I was able to hijack interactions and preform them on the newly created document.
But or you could not close the initial document, nor QCAD, or the application crashed when setting the focus to it.

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”