I wrote a JS script that needs to select a portion from the source file and copy it to a newly created target file

If you are having problems with QCAD, post here. Please report bugs through our Bug Tracker instead.

Always attach your original DXF or DWG file and mentions your QCAD version and the platform you are on.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
hik-IT
Junior Member
Posts: 20
Joined: Wed Jun 11, 2025 2:33 am

I wrote a JS script that needs to select a portion from the source file and copy it to a newly created target file

Post by hik-IT » Tue Aug 12, 2025 7:40 am

I wrote a JS script that needs to select a portion from the source file and copy it to a newly created target file. The attached CroppingDwg.js is the JS code, z.dwg is the source file, and Cropping.xml is the input parameter.

The command I used is:
./qcad -platform offscreen -no-gui -allow-multiple-instances -autostart CroppingDwg.js -f -o z1.dwg z.dwg Cropping.xml

Now I've encountered an issue: When executing the JS via the command, z1.dwg is generated, but the area specified in Cropping.xml is not copied to z1.dwg.

Environment: CentOS 7
Version: v3.32.3-pro
Attachments
z1.dwg
(19.93 KiB) Downloaded 48 times
z.dwg
(437.29 KiB) Downloaded 52 times
CroppingDwg.js
(8.6 KiB) Downloaded 52 times
Cropping.xml
(270 Bytes) Downloaded 43 times

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

Re: I wrote a JS script that needs to select a portion from the source file and copy it to a newly created target file

Post by CVH » Tue Aug 12, 2025 9:02 pm

Hi,

Code remarks in Chinese are not helping, please keep native coding language in English.

There is something wrong with RCopyOperation and RPasteOperation.
Both are are in fact specialized operations that both preform a copy, read more here.

Regards,
CVH

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

Re: I wrote a JS script that needs to select a portion from the source file and copy it to a newly created target file

Post by CVH » Wed Aug 13, 2025 5:00 am

Hi,

At some point it states in Chinese: Get entities within the clipping area
selectionMode is set to insert.selectionMode || "contained"
A boolean OR operation with a string ... Suspecting that the string "contained" is regarded as truly.
A boolean OR True is the same as the boolean itself.

You use doc.queryIntersectedEntitiesXY() to later select these queried ID's.
  • What will return entity ID's that actually cross or intersect with the given box.
The use of fullyContained true/false is wrong here:
  • Target parameter is checkBoundingBoxOnly what diversifies between if the entity or the bounding box is crossing.
    The latter may be easier/quicker with complex entities.
Why excluding entities on locked layers?
  • These are displayed just the same.
    Entities on locked layers are guarded from any change unless the operation allows that by forcing.
The list for filtering on entity type is empty:
  • No type will comply with that. Or use a list with RS.EntityType or the default list.
A document that is recently imported has no selection yet.

:arrow: I think that the intention is to find and select all that are fully contained within a given box: doc.queryContainedEntitiesXY(box)
Endless lines are excluded by default.

To this list one can add those that intersect with the given box: doc.queryIntersectedEntitiesXY(...)
:!: But remind that these are not automatically clipped at the box boundaries.
Clipping to a rectangle is a GUI Pro tool.
Read Pro-prietary ... Typically there is not much information available.
Suspected to use ShapeAlgorithms to preform the trimming at entity intersection points with the box outlines.
One by one while handling those that can be exploded like: Text, Blocks, Dimensions and so on.

:arrow: It is the idea that things are selected before an RPasteOperation if that must act on a selection.
The RCopyOperation is more intended to work with the Clipboard (GUI based).

Regards,
CVH

hik-IT
Junior Member
Posts: 20
Joined: Wed Jun 11, 2025 2:33 am

Re: I wrote a JS script that needs to select a portion from the source file and copy it to a newly created target file

Post by hik-IT » Wed Aug 13, 2025 7:57 am

Thank you for your reply.
How can I modify my JavaScript code to implement the functionality of pasting entity data within given coordinates?

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

Re: I wrote a JS script that needs to select a portion from the source file and copy it to a newly created target file

Post by CVH » Mon Aug 18, 2025 6:33 am

hik-IT wrote:
Wed Aug 13, 2025 7:57 am
How can I modify my JavaScript code to implement the functionality of pasting entity data within given coordinates?
For the entities fully within a certain area is explained above, select them by id from: doc.queryContainedEntitiesXY(box)
But for cropping those that cross you need the 'Clipping to a rectangle' functionality or implement your own code to clip at the box boundaries.
Ensure that you handle all entity types correctly in your custom clipping code.


I have already a problem with your XML ... It lists that the source file is: 565884efb0d445e98bcce5e37b073bbe.dwg
This drawing file will never be found in the current active path.
Remind that if we download your file, it is typically stored in the user download path on a Win system.
It won't be in the launch path.
Even that will be problematic (in my case) as that might be under 'Program Files' what is guarded by the UAC.

I further don't understand why z.dwg is included in the Command line instruction.
This argument is not isolated at all, certainly not as a source file.
The XML file and thus the source drawing (item.src) is defined as last argument.
The output file is defined as the argument with the -o switch and forcing to overwrite with the -f switch, type with the -t switch.


The summary of the 'read more here' link:
With the GUI we use Copy/Paste to copy a portion of one drawing to another.
Both preform a copy ... Copy copies the selection to the Clipboard ... Paste copies the content of the Clipboard to the destination file.
What is already wrong in your code is that the paste operation uses the destination drawing as source :roll: : See reference.

Regards,
CVH

Post Reply

Return to “QCAD Troubleshooting and Problems”