Simulate 'User Coordinate System' using 'rotate' and 'move'.
Posted: Sun Sep 17, 2017 2:10 am
Hi,
The 'User Coordinate System' (UCS) was developed in AutoCAD (and other CAD programs) to help draw 3D models. However, it can also be useful in 2D drawing.
It works by specifying a direction for the X and Y axis, and selecting a point for the origin. In AutoCAD you can save multiple named UCS definitions which you can switch between. In QCAD, Andrew has already added some code in preparation for implementing UCS.
Until then, I thought it might be useful to simulate this by using 'rotate' to set a direction for the X axis (obviously in 2D the Y axis direction isn't used), and to use 'move' to simulate moving the origin. I decided not to use multiple named definitions, but just implement the ability to set a direction for the X axis, and move the origin, and finally to provide a command to return the drawing to its original orientation and location (as it can be tricky to keep track of the origin after multiple moves and rotations).
Example 1. I came up with three commands.
1. 'ucs', shortcut 'UC'.
This allows you to set a direction for the X axis.
Figure 1. The ucs toolbar. Figure 2. The command line prompt. You set the direction in the options toolbar, or you can select an entity and its angle will be put in the direction box. Not all entities have an angle associated with them. For example arcs, circles, polylines etc. do not have an angle property, so if you pick an entity that doesn't have an angle, an error message is shown in the command line, but you can continue to select another entity. Once you have set the angle you can click on the 'Tick' icon to run the command, or alternatively you can press the 'return' or 'enter' key on the keyboard to run the command.
The 'direction' is actually an angle which is aligned with the X axis. If you draw a line parallel to the X axis, going from left to right, it will have the angle represented by the X axis when the drawing is returned to its original orientation. In the example above, setting the direction to 72 will rotate the drawing so that side 2 of the pentagon will be horizontal to the X axis. Any lines drawn parallel to the X axis, going from left to right, will have an angle of 72 degrees. Lines going from right to left will have an angle of 72 + 180 (252) degrees.
(Note: you can use a mathematical expression in the box, so to get side 3 parallel to the X axis you can enter 144 or '2*72').
Also note that the value in the direction box is an absolute value, and not a relative angle.
2. 'ucsorigin', shortcut 'UO'.
This allows you to move the origin.
Figure 3. The ucsorigin toolbar Figure 4. The command line prompt You enter the X and Y coordinates of the point you want as the origin, in the options toolbar. Alternatively you can select the point with the mouse and the coordinates of that point are put in the X and Y coordinate boxes. Once you have set the coordinates, you can click on the 'Tick' icon to run the command, or press the 'return' or 'enter' key om the keyboard.
This command moves the selected point to the origin, and all the entities are moved the same amount.
3. 'ucs2wcs', shortcut 'UW'.
This sets the drawing to its original location and orientation, i.e. move from the 'User Coordinate System' (ucs) to the 'World Coordinate System' (wcs).
This command doesn't require any user input.
As I am sure you have spotted, the main drawback to this approach, is the amount of time it takes to rotate and/or move the entire drawing. On my computer I tested this with two sample drawings. The first drawing contains just over 5,700 entities, and takes 4 seconds to rotate, and 3 seconds to move. The second drawing contains just over 12,000 entities and takes 19 seconds to rotate, and 17 seconds to move. Your computer may be faster or slower than this. So it is up to the user to decide if this is acceptable.
(Note: the larger drawing takes much longer to process because it contains many blocks, hatches, polylines, text blocks and dimensions. Each of these count as one entity, but contain multiple lines, all of which need to be processed).
These commands store the direction and origin in two variables, UCSORG stores the offset to the original origin point, and UCSXDIR stores the direction of the X axis. These variables are stored in the drawing when it is saved. If you save the drawing while it is rotated and/or moved, the variables are stored in the drawing, so you can close down QCAD, and when you reopen the drawing you can use 'UW' to return the drawing to its original orientation and location. I would then save it again.
A cautionary note: Do not use the 'UNDO' command with these commands. Use 'UW' to reverse any changes. The 'UNDO' command will undo a rotation or move, but it doesn't know to change the variables, so the variables and drawing are out of sync.
If this happens, you will have to set the orientation and location manually. Once you have the drawing correct you then need to run a small script file called 'clearucs.js'. This sets the variables to their default values. The script file is in the 'ucs2wcs' directory. Use the menu option 'Misc->Development->Run Script' option (shortcut 'XC'), and select the file and run it. The drawing and variables will now be in sync again, so save the drawing.
I have attached the file 'UCS.zip', which contains three directories (one for each command). Extract these directories to the 'scripts/Edit' directory. The commands appear in the 'Edit' menu.
You can obviously do all this manually, but I think these commands may be useful, especially if you do this often.
Regards
riverbuoy
The 'User Coordinate System' (UCS) was developed in AutoCAD (and other CAD programs) to help draw 3D models. However, it can also be useful in 2D drawing.
It works by specifying a direction for the X and Y axis, and selecting a point for the origin. In AutoCAD you can save multiple named UCS definitions which you can switch between. In QCAD, Andrew has already added some code in preparation for implementing UCS.
Until then, I thought it might be useful to simulate this by using 'rotate' to set a direction for the X axis (obviously in 2D the Y axis direction isn't used), and to use 'move' to simulate moving the origin. I decided not to use multiple named definitions, but just implement the ability to set a direction for the X axis, and move the origin, and finally to provide a command to return the drawing to its original orientation and location (as it can be tricky to keep track of the origin after multiple moves and rotations).
Example 1. I came up with three commands.
1. 'ucs', shortcut 'UC'.
This allows you to set a direction for the X axis.
Figure 1. The ucs toolbar. Figure 2. The command line prompt. You set the direction in the options toolbar, or you can select an entity and its angle will be put in the direction box. Not all entities have an angle associated with them. For example arcs, circles, polylines etc. do not have an angle property, so if you pick an entity that doesn't have an angle, an error message is shown in the command line, but you can continue to select another entity. Once you have set the angle you can click on the 'Tick' icon to run the command, or alternatively you can press the 'return' or 'enter' key on the keyboard to run the command.
The 'direction' is actually an angle which is aligned with the X axis. If you draw a line parallel to the X axis, going from left to right, it will have the angle represented by the X axis when the drawing is returned to its original orientation. In the example above, setting the direction to 72 will rotate the drawing so that side 2 of the pentagon will be horizontal to the X axis. Any lines drawn parallel to the X axis, going from left to right, will have an angle of 72 degrees. Lines going from right to left will have an angle of 72 + 180 (252) degrees.
(Note: you can use a mathematical expression in the box, so to get side 3 parallel to the X axis you can enter 144 or '2*72').
Also note that the value in the direction box is an absolute value, and not a relative angle.
2. 'ucsorigin', shortcut 'UO'.
This allows you to move the origin.
Figure 3. The ucsorigin toolbar Figure 4. The command line prompt You enter the X and Y coordinates of the point you want as the origin, in the options toolbar. Alternatively you can select the point with the mouse and the coordinates of that point are put in the X and Y coordinate boxes. Once you have set the coordinates, you can click on the 'Tick' icon to run the command, or press the 'return' or 'enter' key om the keyboard.
This command moves the selected point to the origin, and all the entities are moved the same amount.
3. 'ucs2wcs', shortcut 'UW'.
This sets the drawing to its original location and orientation, i.e. move from the 'User Coordinate System' (ucs) to the 'World Coordinate System' (wcs).
This command doesn't require any user input.
As I am sure you have spotted, the main drawback to this approach, is the amount of time it takes to rotate and/or move the entire drawing. On my computer I tested this with two sample drawings. The first drawing contains just over 5,700 entities, and takes 4 seconds to rotate, and 3 seconds to move. The second drawing contains just over 12,000 entities and takes 19 seconds to rotate, and 17 seconds to move. Your computer may be faster or slower than this. So it is up to the user to decide if this is acceptable.
(Note: the larger drawing takes much longer to process because it contains many blocks, hatches, polylines, text blocks and dimensions. Each of these count as one entity, but contain multiple lines, all of which need to be processed).
These commands store the direction and origin in two variables, UCSORG stores the offset to the original origin point, and UCSXDIR stores the direction of the X axis. These variables are stored in the drawing when it is saved. If you save the drawing while it is rotated and/or moved, the variables are stored in the drawing, so you can close down QCAD, and when you reopen the drawing you can use 'UW' to return the drawing to its original orientation and location. I would then save it again.
A cautionary note: Do not use the 'UNDO' command with these commands. Use 'UW' to reverse any changes. The 'UNDO' command will undo a rotation or move, but it doesn't know to change the variables, so the variables and drawing are out of sync.
If this happens, you will have to set the orientation and location manually. Once you have the drawing correct you then need to run a small script file called 'clearucs.js'. This sets the variables to their default values. The script file is in the 'ucs2wcs' directory. Use the menu option 'Misc->Development->Run Script' option (shortcut 'XC'), and select the file and run it. The drawing and variables will now be in sync again, so save the drawing.
I have attached the file 'UCS.zip', which contains three directories (one for each command). Extract these directories to the 'scripts/Edit' directory. The commands appear in the 'Edit' menu.
You can obviously do all this manually, but I think these commands may be useful, especially if you do this often.
Regards
riverbuoy