Annotation Style Sheet
In a DWG database, the annotation styles are stored in the annotation style sheet McDbDimStyleTable(), and each record in the annotation style sheet is called the annotation style record object McDbDimStyleTableRecord(). Each annotation style record object corresponds to an annotation style, which is saved along with the drawing. Different drawings may use different annotation styles.
We can obtain the current control by calling MxCpp.getCurrentMxCAD()
in mxcad, and then call the getDatabase() method of the control instance to get the database instance McDbDatabase(). By calling the getDimStyleTable() method in this database instance, we can obtain the annotation style sheet McDbDimStyleTable().
Click Annotation Style Sheet McDbDimStyleTable()、Annotation style record object McDbDimStyleTableRecord()、Database instance McDbDatabase() to view detailed attribute and method descriptions.
The variables for setting the annotation style are consistent with those in AutoCAD. Users can search for the values and names of the relevant setting variables according to their own needs for modifying the annotation style, and then call the setting API provided by mxcad to modify and create a new annotation style.
Click Mark the style system variables to query the details of the annotation style variable data table.
import { MxCpp} from "mxcad"
// Obtain the current control
const mxcad = MxCpp.getCurrentMxCAD();
// Get the current annotation style sheet
const dimStyleTable = mxcad.getDatabase().getDimStyleTable();
Current annotation style
The current annotation style is the default annotation style used by users when adding an annotation object to the DWG database. We can obtain the id of the current annotation style by calling the getCurrentlyDimStyleId() method in the database instance McDbDatabase() object. Based on the annotation style id, we can obtain the corresponding annotation style record object through McObjectId.getMcDbDimStyleTableRecord(), and then get the name of the current annotation style.
Click McDbDatabase.getCurrentlyDimStyleId() View detailed attribute and method descriptions.
import { MxCpp } from "mxcad"
const mxcad = MxCpp.getCurrentMxCAD();
const dimStyleId = mxcad.getDatabase().getCurrentlyDimStyleId();
console.log(" Current Annotation Style id", dimStyleId);
const dimStyleName = dimStyleId.getMcDbDimStyleTableRecord().name;
console.log(" Current annotation style name ", dimStyleName);
Add annotation styles
We can directly call the addDimStyle() method in the mxcad instance object to add the annotation style, and then set the drawDimStyle property to set the added annotation style to the style of the current drawing annotation.
Click McObject.addDimStyle()、 McObject.drawDimStyle() to view detailed attribute and method descriptions.
import { MxCpp } from "mxcad"
let mxcad = MxCpp.getCurrentMxCAD();
//Clear the currently displayed content
mxcad.newFile();
//Set the style of the annotated text within the annotation style
let textId = mxcad.addTextStyle("My annotated font", "complex.shx", "gbcbig.shx", 1);
//Create a new annotation style
mxcad.addDimStyle("MyDimStyle", "41,0.18,141,0.09,40,200", "77,1,271,3", '3,"DN%%c<>"', `340,${textId.id}`);
// Set the current annotation style
mxcad.drawDimStyle = "MyDimStyle"
In addition, we can also obtain the annotation style sheet McDbDimStyleTable() in the current database, and then add an annotation style record object McDbDimStyleTableRecord() to the style sheet.
import { McDbAlignedDimension,McDbDimStyleTableRecord, MxCpp, McGePoint3d } from "mxcad";
const mxcad = MxCpp.getCurrentMxCAD();
const dimStyleTable = mxcad.getDatabase().getDimStyleTable()
let newRecord = new McDbDimStyleTableRecord();
// Set the name of the annotation style
newRecord.name = 'myTestDimStyle';
// Set integer data
newRecord.setDimVarInt(77,1);
newRecord.setDimVarInt(271,3);
// Set double-precision data
newRecord.setDimVarDouble(78,8);
newRecord.setDimVarDouble(41,0.18);
newRecord.setDimVarDouble(141,0.09);
newRecord.setDimVarDouble(40,200);
// Set the string type data
newRecord.setDimVarString(3,"DN%%c<>");
// Add the annotation style to the annotation style sheet
const id = dimStyleTable.add(newRecord);
// Call the new annotation style to draw the annotation
const mDimension = new McDbAlignedDimension()
mDimension.xLine1Point = new McGePoint3d(-1800, 800);
mDimension.xLine2Point = new McGePoint3d(1800, 800);
mDimension.dimLinePoint = new McGePoint3d(800, 500);
mDimension.dimensionStyle = id;
mxcad.drawEntity(mDimension)
Traverse all the annotation styles
We can obtain the ids of all the annotation styles by calling the getAllRecordId() method in the annotation style sheet McDbDimStyleTable(). Call again getMcDbLinetypeTableRecord () method returns the linear table record McDbLinetypeTableRecord (), all data linear style.
import { MxCpp } from "mxcad"
// Obtain the current cad object
let mxcad = MxCpp.getCurrentMxCAD();
// Get the annotation style sheet
let dimTypeTable = mxcad.getDatabase().getDimStyleTable();
// Obtain the ids of all record objects in the annotation style sheet
let aryId = dimTypeTable.getAllRecordId();
// Traverse the annotation style to record the object id
aryId.forEach((id) => {
let dimTypeRec = id.getMcDbDimStyleTableRecord();
if (dimTypeRec === null) return;
console.log(dimTypeRec);
console.log("Mark the style name:" + dimTypeRec.name);
});
Delete the annotation style
After we obtain the target annotation style sheet record object McDbDimStyleTableRecord(), we can call the erase() method of this object instance to delete the object.
import { MxCpp } from "mxcad";
// Obtain the current mxcad object
const mxcad = MxCpp.getCurrentMxCAD();
// Get the annotation style sheet
let dimStyleTable = mxcad.getDatabase().getDimStyleTable()
let dimStyleId = dimStyleTable.get("Target annotation style name");
if(dimStyleId.isValid()) dimStyleId.erase();
// Update display
mxcad.updateDisplay()
Modify the annotation style
In mxcad, to modify the annotation style of an annotation, you can directly delete the original annotation style, then add a new definition of the annotation style, and finally set the annotation style of the target object to the newly added annotation style. Alternatively, you can directly modify the relevant Settings of the annotation style sheet record object based on the original annotation style. The following takes the example of having a user select a annotation object on a CAD drawing and then operate on all the annotation styles applied to the annotation object
import { MxCADUiPrEntity,MxCpp } from "mxcad";
const mxcad = MxCpp.getCurrentMxCAD()
// Select an object
let selEntity = new MxCADUiPrEntity();
selEntity.setMessage("Select an object");
let id = await selEntity.go();
if (!id.isValid()) return;
// Obtain the object
let ent = id.getMcDbEntity();
if (ent === null) return;
// Modify the annotation style
id.getMcDbDimension().setDimVarInt(176,122);
id.getMcDbDimension().setDimVarInt(177,122);
id.getMcDbDimension().setDimVarInt(178,122);
mxcad.updateDisplay()