Distance from point to line
Below we will introduce how to use mxcad plug-in to achieve the function of measuring the distance from the point to the line in the CAD drawing. In this function, after the user selects the measuring point and the target line, the vertical distance from the point to the line will be marked. The measuring point to straight line distance function can help users quickly grasp the vertical distance between the target point and the straight line, and it is convenient to count the engineering quantity.
Function implementation
- Take some points
We can use MxCADUiPrPoint take the object selected on the drawing need to measure the target object.
const getPt = new MxCADUiPrPoint();
getPt.setMessage('Please select a measuring point');
const pt = await getPt.go();
if (!pt) return;
- Obtain the target line object
We can choose using entity object MxCADUiPrEntity() According to the coordinates of the user's mouse click to get the corresponding entity, where we need to select only the line object, so, We'll call MxCADResbuf() to select entity object set filter to filter out the target entity.
// Set filter
const filter = new MxCADResbuf([DxfCode.kEntityType, "LINE"]);
const getEnt = new MxCADUiPrEntity();
getEnt.setMessage('Please select a straight line')
getEnt.setFilter(filter);
const entId = await getEnt.go();
if (!entId.id) return;
const line = entId.getMcDbEntity() as McDbLine;
- Draw distance label
In mxcad linear object corresponding to the entity class to McDbLine, This class provides properties or methods to get or set information about a line, which we can choose to call according to our functional requirements. In the function of measuring the distance from the point to the line, We can call McDbLine.getClosestPointTo() Method obtains the vertical point from the target point to the target line object, and the distance from the vertical point to the target point object is the distance from the point to the line. Finally, we call again mxcad.drawDimAligned() Draw alignment marks.
// Acquisition sag
const verPt = line.getClosestPointTo(pt, true).val;
let id!: McObjectId;
const mxcad = MxCpp.getCurrentMxCAD();
const getPos = new MxCADUiPrPoint();
getPos.setMessage('Please set the position of the dimension line');
getPos.setUserDraw((point, pw) => {
id && id.erase()
id = mxcad.drawDimAligned(pt.x, pt.y, verPt.x, verPt.y, point.x, point.y);
});
const position = await getPos.go();
if (!position){
id && id.erase()
return
};
id && id.erase();
mxcad.drawDimAligned(pt.x, pt.y, verPt.x, verPt.y, position.x, position.y);
Functional practice
Practical effects are as follows:
- Click the Measure Circle button to perform the measure circle method
- Select the target circle object
- Set the point location
- The contents of the measurement annotations were successfully drawn