Skip to content
On this page

Rotate

Below we will show how to use the mxcad plug-in to implement the rotation function in CAD drawings. In this function, the user can box select or click the target entity to rotate it for editing the content of the drawing. In our document Common Editing operations has done a basic explanation of the rotation operation, the following we will achieve the rotation function that can interact with users on this basis.

Function implementation

  1. Select the target entity

We can call MxCADSelectionSet Select the entity that you want to rotate.

ts
// Select the object you want to mirror
let ss = new MxCADSelectionSet();
if (!await ss.userSelect("\n Select the rotate object")) return;
  1. Set the rotation Angle

We by calling MxCADUiPrPoint take the object to determine the rotation of the object points. Call again MxCADUiPrAngle for users to set of object rotation Angle. During the setup process, We can go through MxCADUiPrAngle.setUserDraw() Dynamically drawing the image object enables us to see the position of the rotated object more intuitively. Since the redrawing function needs to be continuously performed during the dynamic drawing process, in order to improve the rendering speed and reduce the drawing problem caused by too many rotating objects, We can temporarily consolidate the target object into a block McDbBlockTableRecord, In the process of dynamic rendering, the initial rotation is carried out in the form of image blocks, and the target object is rotated after the final rotation Angle is determined.

ts
// Set block
let blkTable =  mxcad.getDatabase().getBlockTable();
let blkRecId = blkTable.add(new McDbBlockTableRecord());
let blkTableRecord:McDbBlockTableRecord = blkRecId.getMcDbBlockTableRecord() as any;
if(blkTableRecord == null) return;
ss.forEach((id)=>{
    let ent = id.getMcDbEntity();
    if(!ent) return;
    let cent = ent.clone() as McDbEntity;
    blkTableRecord.appendAcDbEntity(cent);
})
// Set the block base point
blkTableRecord.origin = basePt;
let blkRef = new McDbBlockReference();
blkRef.blockTableRecordId = blkRecId;
// Set the block position
blkRef.position = basePt;


// Specified rotation Angle
let getAngle = new MxCADUiPrAngle();
getAngle.setMessage('Please specify the rotation Angle')
getAngle.setBasePt(basePt);
getAngle.setUserDraw((pt, pw) => {
    pw.drawLine(pt.toVector3(), basePt.toVector3());
    // Dynamically rotate the block
    let a = pt.sub(basePt).angleTo2(McGeVector3d.kXAxis, McGeVector3d.kNegateZAxis);
    const _clone = blkRef.clone() as McDbBlockReference;
    _clone.rotation = a;
    pw.drawMcDbEntity(_clone)
})
let val = await getAngle.go();
if (!val) return;
const angle = getAngle.value();
  1. Rotate the object

In the above step, we obtained the rotation Angle, so, We can direct call McDbEntity.rotate() method to rotate the object. After moving, Call McObject.updateDisplay() method to update the drawing shows.

ts
const mxcad = MxCpp.getCurrentMxCAD();
// Mirror image rotating entity
ss.forEach(id => {
    let ent = id.getMcDbEntity();
    if (!ent) return;
    ent.rotate(basePt, angle);
})
// Update display
mxcad.updateDisplay();

Functional practice

Practical effects are as follows:

  • Click the rotation button to perform the rotation method
  • Left click to select the target entity, right click to end the selection (or direct box selection)
  • Click the left button to set the rotation point according to the command line prompt
  • Move the mouse and click the left button again to set the rotation Angle (or enter the Angle value directly in the input box and press Enter)
  • Successfully rotated the object