Line table
In the DWG database, the line style is stored in the line style sheet McDbLinetypeTable(), and each record in the line style sheet is called the line table record object McDbLinetypeTableRecord(). Each line table record object corresponds to a line style, which is saved with the drawing, and the line style used by different drawings may be different.
We can get 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 getLinetypeTable() in this database instance, we get the linear style sheet McDbLinetypeTable().
Click on linear stylesheet McDbLinetypeTable(), linear table record object McDbLinetypeTableRecord(), the database instance McDbDatabase() to check the detailed description attributes and methods.
import { MxCpp} from "mxcad"
// Get the current control
const mxcad = MxCpp.getCurrentMxCAD();
// Get the current line style sheet
const lineTypeTable = mxcad.getDatabase().getLinetypeTable();Current line type
The current line type represents the default line type style used by the user when adding an entity object to the DWG database. We can obtain the name of the current line style and the text style id by calling the getCurrentlyLineTypeName() method in the database instance McDbDatabase() object and the getCurrentlyLinetypeId() method.
Click on McDbDatabase.getCurrentlyLinetypeId() to check the detailed description attributes and methods.
import { MxCpp } from "mxcad"
const mxcad = MxCpp.getCurrentMxCAD();
const lineTypeName = mxcad.getDatabase().getCurrentlyLineTypeName();
const lineTypeId = mxcad.getDatabase().getCurrentlyLinetypeId();
console.log("Current line type", lineTypeName, lineTypeId)In addition, mxcad also provides McDbDatabase.getCurrentlyLineTypeScale() to get the current line ratio。
const lineTypeScale = mxcad.getDatabase().getCurrentlyLineTypeScale();
console.log('The current linear proportion value', lineTypeScale);Add line lines
We can add a line style directly by calling the addLinetype() method or the addLinetypeEx() method in the mxcad instance object, and then set the drawLinetype property to set the added line style to the current drawn line style.
Click on McObject.addLinetype()、 McObject.addLinetypeEx() to check the properties and methods in detail.
import { MxCpp } from "mxcad"
const mxcad = MxCpp.getCurrentMxCAD()
// Define dashed data,"MyLineType" is the line name," 6,-8" is a unit definition of the dashed line, 6 is the length of the solid line, and -8 is the length of the space.
mxcad.addLinetype("MyLineType", "6,-10");
Mxcad. AddLinetypeEx (" TestMyLine ", '. 5, -. 2, [" HW ", STANDARD, S = 1, R = 0.0, X = 0.1, Y = - 05], -. 2 ', "");
// Design current line type as "MyLineType"
mxcad.drawLinetype = "MyLineType";Alternatively, we can add a line table record object, McDbLinetypeTable(), to the style sheet by taking the linear style sheet McDbLinetypeTableRecord() in the current database.
import { McDbLine, MxCpp, McDbLinetypeTableRecord, McGePoint3d } from "mxcad"
let mxcad = MxCpp.getCurrentMxCAD();
// Set the length of the dotted line
let dashline = mxcad.mxdraw.viewCoordLong2Cad(10);
// Gets the mxcad data object
let databse = mxcad.database;
// Get global linear proportions
let lcale = mxcad.getSysVarDouble("LTSCALE");
// Adjust the length of dashed lines according to the proportion of global lines
dashline = dashline / lcale;
// Gets the current line table
let lineTable = databse.getLinetypeTable();
// Adds a line to the line table
let lineTypeName = "MyTest1";
let lineRecId = lineTable.get(lineTypeName);
if(lineRecId. isNull()){
let lineTypeRecord = new McDbLinetypeTableRecord();
lineTypeRecord.numDashes = 2;
lineTypeRecord. setDashLengthAt(0,dashline);
lineTypeRecord.setDashLengthAt(1,-dashline);
lineTypeRecord.name = lineTypeName;
lineRecId = lineTable. add(lineTypeRecord);
}
// Call the newly added line to draw a straight line
let myline = new McDbLine();
myline.startPoint = new McGePoint3d(0,0,0);
myline.endPoint = new McGePoint3d(500,500,0);
myline.linetypeId = lineRecId;
mxcad.drawEntity(myline);Adding linetypes that reference font files
In the addLinetypeEx() linetype definition, besides dash segments and text segments (such as ["HW",STANDARD,S=.1,...] which references a text style), shape segments that directly reference shx font files are also supported. The format of a shape segment is [text-content,font-file.shx,s=scale,R=rotation,X=X offset,Y=Y offset], and the text in the shape segment is drawn along the line as shapes by the referenced shx font file.
Before using such a linetype, place the font files referenced by the shape segments into the fonts folder of the front-end project (the directory that fontspath points to when creating the mxcad control), and notify mxcad to load the font files with addNetworkLoadingFont(), then wait for the fonts to finish loading with loadFonts().
Click McAppType.addNetworkLoadingFont(), McAppType.loadFonts() to check the properties and methods in detail.
import { MxCpp } from "mxcad";
// Wrap loadFonts in a Promise to wait for the fonts to finish loading
function loadFonts(vecShxFonts: string[], vecBigShxFonts: string[], vecTureTypeFonts: string[]){
return new Promise<boolean>((res, rej) => {
MxCpp.App.loadFonts(vecShxFonts, vecBigShxFonts, vecTureTypeFonts, () => {
res(true);
});
});
}
// Load a linetype definition that contains font files
async function drawLine() {
MxCpp.App.addNetworkLoadingFont(["demo1.shx", "demo2.shx"]);
await loadFonts(["demo1.shx","demo2.shx"],[],[]);
let mxcad = MxCpp.getCurrentMxCAD();
// Clear the current display content
mxcad.newFile();
// Add a linetype that contains shapes
// "YUNXINGTIELU1,demo1.shx,s=1" is a shape segment:
// YUNXINGTIELU1 is the text content drawn as shapes, demo1.shx is the referenced font file, s=1 is the shape scale
MxCpp.getCurrentMxCAD().addLinetypeEx("MyLineTypeText", "0,-5,[YUNXINGTIELU1,demo1.shx,s=1],-10,[YUNXINGTIELU2,demo2.shx,s=1],-5", "");
mxcad.drawLinetype = "MyLineTypeText";
mxcad.drawLineWidth = 0;
mxcad.drawLine(350, 120, 600, 120);
// Put all entities into the current display view
mxcad.zoomAll();
// Update the display of the view
mxcad.updateDisplay();
};
// Call the draw line method
drawLine();Traversing all lines
We can get the ids of all linear styles by calling the getAllRecordId() method in the linear style sheet McDbLinetypeTable(), Call again getMcDbLinetypeTableRecord () method returns the linear table record McDbLinetypeTableRecord (), all data linear style.
import { MxCpp } from "mxcad"
// Get the current cad object
let mxcad = MxCpp.getCurrentMxCAD();
// Get the line table
let linetypeTable = mxcad.getDatabase().getLinetypeTable();
// Gets all record object ids in the line table
let aryId = linetypeTable.getAllRecordId();
// Traversal linear record object id
aryId.forEach((id) => {
let linetypeRec = id.getMcDbLinetypeTableRecord();
if (linetypeRec === null) return;
console.log(linetypeRec);
console.log("linetypeRec.name:" + linetypeRec.name);
});Delete line lines
After we get the target linear table record object McDbLinetypeTableRecord(), we can call the erase() method of the object instance to delete the object.
import { MxCpp } from "mxcad"
// Get the line table
let linetypeTable = MxCpp.getCurrentMxCAD().getDatabase().getLinetypeTable()
let linetypeId = linetypeTable.get(" Target line style name ")
linetypeId.erase()
// Update the display
mxcad.updateDisplay()Modify the line type
The basic operation of modifying line line in mxcad is to delete the original line line, re-add the defined line line, and finally set the target object to the new line line. Let the user select a text object on a CAD drawing, and then manipulate the linear style applied to the object as an example:
import { MxCADSelectionSet, MxCADUiPrPoint, MxCADResbuf } from "mxcad"
let mxcad = MxCpp.getCurrentMxCAD();
// Delete the existing "TestMyLine" line from the current diagram and add a new definition line for mxcad.addLinetypeEx.
let linetypeRecord = mxcad.database.getLinetypeTable().get("TestMyLine");
if(! linetypeRecord.isErase()){
linetypeRecord.erase();
}
// Add the line definition TestMyLine
Let lintype = mxcad. AddLinetypeEx (" TestMyLine ", '. 5, -. 2, [" HW ", STANDARD, S = 1, R = 0.0, X = 0.1, Y = - 05], -. 2 ', "");
if(! lintype.isValid() )return;
// Modify the target object line
let getEnt = new MxCADUiPrEntity();
let entId = await getEnt.go();
let ent = entId.getMcDbEntity();
if(!ent) return;
ent.linetypeId = lintype;Resetting the linetype scale
When drawing CAD drawings, the global linetype scale (LTSCALE) is often not suitable for individual objects—the linetype segments on a long line may be too dense, and those on a short line too sparse. We can recalculate the object's linetype scale (linetypeScale) based on the object's curve length and the total segment length in the linetype definition, so that the target object displays a specified number of complete linetype segments at its current size.
Click McDbLinetypeTableRecord.dashLengthAt(), McDbCurve.getLength(), McDbEntity.linetypeScale, MxCADUtilityClass.selectEnt() to check the properties and methods in detail.
import { McDbCurve, McDbEntity, MxCADUtility, MxCpp } from "mxcad";
// Recalculate the object's linetype scale from the current scale, the curve length and the linetype definition data
function MxReCalcLineTypeScale(ent: McDbEntity, num: number) {
let lineRecord = ent.linetypeId.getMcDbLinetypeTableRecord();
if (!lineRecord) return;
// Only curve objects can have their length calculated
if (!(ent instanceof McDbCurve)) return;
let curve: McDbCurve = ent as McDbCurve;
let len = curve.getLength();
if (!len.ret || len.val <= 0.00000000000001) return;
// Sum of the lengths of all segments in the linetype (negative values are gaps, take the absolute value)
let dashline = 0;
for (let i = 0; i < lineRecord.numDashes; i++) {
let dl = lineRecord.dashLengthAt(i);
if (dl < 0) dl = -dl;
dashline += dl;
}
if (dashline == 0) return;
let mxcad = MxCpp.mxcad;
// Global linetype scale
let lcale = mxcad.getSysVarDouble("LTSCALE");
// Curve length
let realLen = len.val;
// Displayed length of num linetype segments
let linetypelen = dashline * lcale * num;
// The expected current size divided by the displayed length gives the entity's linetype scale
let entLineTypeScale = realLen / linetypelen;
ent.linetypeScale = entLineTypeScale;
}
async function Mx_ResetLineTypeScale() {
let retIds = await MxCADUtility.selectEnt("select");
if (retIds.length == 0) {
return;
}
let id = retIds[0];
let ent = id.getMcDbEntity();
if (ent) {
// 30 is the number of linetype segments expected to be displayed on the object
MxReCalcLineTypeScale(ent, 30);
}
}
Mx_ResetLineTypeScale()