How to open a defined dwg file
Moderator: andrew
Forum rules
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
How to open a defined dwg file
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
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
Re: How to open a defined dwg file
Hi,
First, one has to include the NewFile class high in your code with:
One should check upfront if the given filename exist.
As example:
Then we can open the file as a new tab in the GUI with:
Regards,
CVH
First, one has to include the NewFile class high in your code with:
Code: Select all
include("scripts/File/NewFile/NewFile.js");
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;
}
Code: Select all
// Open the drawing file:
var w = NewFile.createMdiChild(fileName); // Returns a mdiChild
CVH
Last edited by CVH on Sun Oct 17, 2021 3:32 am, edited 2 times in total.
Re: How to open a defined dwg file
Hi CVH,
thanks again for your precious help ( and patience! ).
Gianfranco
thanks again for your precious help ( and patience! ).
Gianfranco
-
- Junior Member
- Posts: 17
- Joined: Thu Sep 07, 2023 1:17 am
Re: How to open a defined dwg file
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
What do I have to do to get actions to affect the new drawing?
Code: Select all
doc = w.getDocument();
di = w.getDocumentInterface();
Re: How to open a defined dwg file
Hi,
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
Then for example query entities from newDoc or send operations to the newDi.
Search the forum for "setActiveSubWindow" for more info on switching between drawing tabs.
Regards,
CVH
Your script actions probably run in the context of the former active drawing.mrhickman53 wrote: ↑Sat Sep 23, 2023 4:11 amWhat do I have to do to get actions to affect the new 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();
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
-
- Junior Member
- Posts: 17
- Joined: Thu Sep 07, 2023 1:17 am
Re: How to open a defined dwg file
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
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
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.
Image demonstrating that the entity to be pasted is not previewed in the target subWindow
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. 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
Code: Select all
var doc = this.getDocument();
var di = this.getDocumentInterface();
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();
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.
Image demonstrating that the entity to be pasted is not previewed in the target subWindow
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. 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
Re: How to open a defined dwg file
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
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
-
- Junior Member
- Posts: 17
- Joined: Thu Sep 07, 2023 1:17 am
Re: How to open a defined dwg file
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
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.
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.
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();
Code: Select all
Paste.prototype.beginEvent.call(this);
Again, thanks for your patience with a newbie.
Re: How to open a defined dwg file
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.
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
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.
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
-
- Junior Member
- Posts: 17
- Joined: Thu Sep 07, 2023 1:17 am
Re: How to open a defined dwg file
I have modified ImportFile to TestAP, which shows the same behavior as my ImportAP.
I have experimented with
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.
I have experimented with
Code: Select all
var myChild = NewFile.createMdiChild(template);
this.setDocumentInterface(myChild.getDocumentInterface());
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 173 times
Re: How to open a defined dwg file
Had a quick look ...
TestAP.js .. Line 65-66:
While Paste.js does the same a split second later ... Line 69:
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
TestAP.js .. Line 65-66:
Code: Select all
this.doc = this.getDocument();
this.di = this.getDocumentInterface();
Code: Select all
this.di = this.getDocumentInterface();
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
-
- Junior Member
- Posts: 17
- Joined: Thu Sep 07, 2023 1:17 am
Re: How to open a defined dwg file
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.
Re: How to open a defined dwg file
Probably the better solution.mrhickman53 wrote: ↑Sun Sep 24, 2023 7:45 pmI have taken a new approach which takes advantage of the NewFile.addPostOpenAction() method.
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