Entity
All objects of a drawing are inherited from the class McDbObject, which provides functions to get the object handle, ID, object type information, and so on. The visible objects on the graph are inherited from the McDbEntity class, which provides operations with common properties of the visible entity, such as lines, layers, text styles, colors, and other functions.
Click on the database objects McDbObject, entity object McDbEntity to check the detailed description attributes and methods.
Entity ID and handle
ID and handle are used to identify an object, ID is a 64-bit long integer variable, is a memory address, the fastest access, but it is different every time you open, if you need to store the identity of an object, the next time you can find the object, you need to use a handle, a handle is a string variable, It guarantees that no matter what time, the handle is unique in the DWG drawing, and the ID is unique in memory.
We get the object handle from the getHandle() method provided in the McDbObject class, and the object ID from the getObjectID() method.
tap McDbObject getHandle(), McDbObject getOb JectID() to check the detailed description attributes and methods.
// Select a target object
const getEnt = new MxCADUiPrEntity();
getEnt.setMessage(' Select target object ');
const entVal = await getEnt.go();
if (! entVal.id) return;
// Gets the entity of the object
const ent = entVal.getMcDbEntity();
if(! ent) return;
// Get the object ID
const entId = ent.getObjectID();
// Get the object handle
const sHandle = ent.getHandle();
console.log(" Object id", entId);
console.log(" object handle ", sHandle);
Type information of the entity
We can get the type name of the object by calling the objectName attribute provided in the McDbObject class. With the type name, we can determine what object the entity is, and then call the dxf0 attribute to get the type name of the DXF group code of the object. This is the same as the DXF group code in AutoCAD. For example, the LINE type name: McDbLine,DXF0 group code value: LINE,DXF0 group code value can be used to construct a set of type filtering.
Click McDbObject.objectName、McDbObject.dxf0 to view detailed description attributes and methods.
// Let the user select an object on the graph.
const getEnt = new MxCADUiPrEntity();
getEnt.setMessage(' Select target object ');
const entVal = await getEnt.go();
if (! entVal.id) return;
// Get the object entity
const ent = entVal.getMcDbEntity();
// Get the value of Dxf0.
const sDxfType = ent?.dxf0;
console.log("Dxf0值", sDxfType)
if (ent?.objectName == "McDbLine"){
// ent is a straight line
const line = ent as McDbLine;
// Get the start point of the line
const pt1 = line.startPoint;
// Get the end point of the line
const pt2 = line.endPoint;
console.log(pt1, pt2);
}else if(ent?.objectName == "..."){
// Other types
}
Visible entity properties
The main attributes of McDbEntity can be seen on the figure, including line line, layer, text style, color, whether it is visible, display order, etc. We can extract and modify these properties.
- Set whether the entity is visible
// Hide the entity
ent.visible = false;
- Change the object color
// Select the target object
let getEntity = new MxCADUiPrEntity();
getEntity.setMessage(" Select target object ");
let id = await getEntity.go();
if (! id.isValid()) return;
let ent = id.getMcDbEntity();
if (! ent) return;
// Change the object color
ent.trueColor = new McCmColor(255, 0, 255);
- Change the display sequence
When a user draws, by default, overlapping objects (such as text, wide polylines, and entity fill polygons) are usually displayed in the order in which they were created: newly created objects appear in front of existing ones. This example demonstrates how to control the display order, the specific implementation code is as follows:
let ss = new MxCADSelectionSet();
if (! await ss.userSelect("\n select object ")) return;
// Get the maximum and minimum display order of the objects on the current graph.
let minmaxOrder = MxCpp.getCurrentDatabase().currentSpace.getMinMaxDrawOrder();
// Put the object at the top.
let lOrder = minmaxOrder.maxDrawOrder + 1;
ss.forEach((id) => {
let ent = id.getMcDbEntity();
if (ent) {
ent.drawOrder = lOrder;
}
})
Extend the data
Users can set extended data to the objects on the drawing. These extended data can be professional data that users actually need. Extended data supports string, double, int and other types. Each extended data has an extended data name, each name may also have multiple extended data, an entity can have multiple extended data names, these extended data in memory is a linked list MxCADResbuf to store, each link represents a data, The string data length in each link does not exceed 200(as determined by the DWG drawing format).
Click list MxCADResbuf to check the properties and methods in detail.
// Let the user select an object on the graph.
const getEnt = new MxCADUiPrEntity();
getEnt.setMessage(' Select target object ');
const entVal = await getEnt.go();
if (! entVal.id) return;
// Get the object entity
const ent = entVal.getMcDbEntity();
if(! ent) return;
const xData = new MxCADResbuf();
// Set the extended data name TestApp
xData.AddString("TestApp",1001);
// Set string data, DXF group code is 1000;
xData.AddString("string data", 1000);
// Set a Long data, DXF group code is 1071
xData.AddLong(77677, 1071);
// Write the prepared data to the entity.
ent.setxData(xData);
Edit entity
We provide functions for editing entities such as copy, pan, zoom, transform, mirror orientation, and rotation. Below to copy the translation entity, for example, more editing function, please refer to McDbEntity.
- Copy translation
import { MxCpp, MxCADUiPrEntity, MxCADUiPrPoint, McGeMatrix3d} from "mxcad";
// Copy translation
export async function test_copyMove() {
// Get canvas instance
const mxcad = MxCpp.App.getCurrentMxCAD();
let copyObj = new MxCADUiPrEntity();//Select copy object
copyObj.setMessage('Select the object you want to copy')
let copyObj_id = await copyObj.go();
if (copyObj_id === null) return;
let copyEvent: any = await copyObj_id.getMcDbEntity()//Copy the instance object
if (!copyEvent) return;
let getFristPoint = new MxCADUiPrPoint();
getFristPoint.setMessage('Please set the copy base point')
let fristPoint: any = await getFristPoint.go();//Cardinal point
if (!fristPoint) return;
let getNextPoint = new MxCADUiPrPoint();
getNextPoint.setMessage('Please set the length and direction of the move');
// Dynamic rendering
getNextPoint.setUserDraw((pt, pw) => {
let evrnt_clone = copyEvent.clone();// Replication entity
let matrix = new McGeMatrix3d();// Create a matrix
matrix.setToTranslation(pt.sub(fristPoint));//Set movement matrix
evrnt_clone.transformBy(matrix);// Transform entity
pw.drawMcDbEntity(evrnt_clone);// Dynamically draws the translated entity
let pl = new McDbPolyline();// Create a multiline object
pl.addVertexAt(fristPoint);
pl.addVertexAt(pt);
pw.drawMcDbEntity(pl);// Plot translation path
});
let nextPoint = await getNextPoint.go();
if (!nextPoint) return;
// Translation transformation
let matrix = new McGeMatrix3d();// Create a matrix
let evrnt_clone = copyEvent.clone();
matrix.clone();
matrix.setToTranslation(nextPoint.sub(fristPoint));//Set movement matrix
evrnt_clone.transformBy(matrix);// Transform entity
mxcad.drawEntity(evrnt_clone);// Draw the translated entity
}
Delete the entity
We can delete the entity directly by calling the erase() method in the McDbEntity class via the entity object or entity object id.
Click McDbEntity erase() to check the properties and methods in detail.
// Select the target object
let getEntity = new MxCADUiPrEntity();
getEntity.setMessage(" Select target object ");
let id = await getEntity.go();
if (!id.isValid()) return;
id.erase();
// Select the target object
let getEntity = new MxCADUiPrEntity();
getEntity.setMessage(" Select target object ");
let id = await getEntity.go();
if (! id.isValid()) return;
// Get the object entity
const ent = id.getMcDbEntity();
ent.erase();
Get the physical coordinates
Different entities have different coordinate properties, such as a straight line is the start point, and the end point property, and a circle is the center point coordinate and radius property. So to get the coordinates of the entity, you need to determine the object type, and then convert to a specific object, and then read its coordinates. We can get the object's minimum outsourced rectangle coordinates by calling the getBoundingBox() method in the McDbEntity class.
Click on McDbEntity getBoundingBox() View detailed property and method descriptions.
// Let the user select an object on the graph.
const getEnt = new MxCADUiPrEntity();
getEnt.setMessage(' Select target object ');
const entVal = await getEnt.go();
if (! entVal.id) return;
// Get the object entity
const ent = entVal.getMcDbEntity();
if(! ent) return;
// Get the bounding box of the entity
const {ret, minPt, maxPt} = ent.getBoundingBox();
if(ret){
// Sets the display range to the size of the bounding box
MxCpp.getCurrentMxCAD().zoomW(minPt, maxPt);
}