Skip to content
On this page

Text style sheet

In the DWG database, the text style is stored in the text style sheet McDbTextStyleTable(), and each record in the text style sheet is called the text style sheet record object McDbTextStyleTableRecord(). Each text style sheet record object corresponds to a text style, you can set the text style properties: font file name, large font file name, new text height, new text width scaling ratio, text tilt Angle, TrueType font data. Among them, the text style is saved with the drawing, and the text style used by different drawings may be different. The DWG database always has a "Standard" text style by default, which cannot be deleted.

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 the getTextStyleTable() method in this database instance, we get the literal style sheet McDbTextStyleTable().

Click on writing stylesheets McDbTextStyleTable(), text style sheets record object McDbTextStyleTableRecord(), the database instance McDbDatabase() to check the detailed description attributes and methods.

ts
import { MxCpp} from "mxcad"
// Get the current control
const mxcad = MxCpp.getCurrentMxCAD();
// Get the current text style sheet
const textSyleTable = mxcad.getDatabase().getTextStyleTable();

Current text style

The current text style represents the default text style used when adding a text object to the DWG database. We can by calling the database instance McDbDatabase () object of getCurrentlyTextStyleName () method gets the current text style name and getCurrentlyTextStyleId () method to obtain the text style id.

Click McDbDatabase.getCurrentlyLayerId() to view detailed property and method descriptions.

ts
import { MxCpp } from "mxcad"

const mxcad = MxCpp.getCurrentMxCAD();
const textStyleName = mxcad.getDatabase().getCurrentlyTextStyleName();
const textStyleId = mxcad.getDatabase().getCurrentlyTextStyleId();
console.log("Current text style", textStyleName, textStyleId)

Add a text style

We can add the text style directly by calling the addTextStyle() method in the mxcad instance object, and then set the drawTextStyle property to set the added text style to the current drawn text style.

Click McObject addTextStyle() to check the properties and methods in detail.

ts
import { McApp } from "mxcad"

const mxcad = McApp.getCurrentMxCAD()
mxcad.addTextStyle("MyLineTypeTextStyle", "txt.shx", "hztxt.shx", 1);
mxcad.drawLayer = "MyLineTypeTextStyle"

In addition, we can add a text style sheet record object McDbTextStyleTableRecord() to the style sheet by taking the text style sheet McDbTextStyleTable() in the current database.

ts
import { McCmColor, MxCpp, McDbTextStyleTableRecord, McDb } from "mxcad"

const mxcad = MxCpp.getCurrentMxCAD();
// Get the current text style sheet
const textSyleTable = mxcad.getDatabase().getTextStyleTable();
let newTextStyleRecord = new McDbTextStyleTableRecord();
newTextStyleRecord.fileName = "txt.shx";
newTextStyleRecord.bigFontFileName = "hztxt.shx";
newTextStyleRecord.textSize = 10;
newTextStyleRecord.name = sMyTextStyle;
newTextStyleRecord.xScale = 0.7;
if (textSyleTable.add(newTextStyleRecord).isValid()) {
console.log("add ok");
}

Go through all text styles

We can get the ids of all text styles by calling the getAllRecordId() method in the text style table McDbTextStyleTable(), Call again getMcDbTextStyleTableRecord () method returns the text style sheets record object McDbTextStyleTableRecord (), get all writing style data.

ts
import { MxCpp } from "mxcad"

let mxcad = MxCpp.getCurrentMxCAD();
let textSyleTable = mxcad.getDatabase().getTextStyleTable();
let aryId = textSyleTable.getAllRecordId();
aryId.forEach((id) => {
    let textSyleRec = id.getMcDbTextStyleTableRecord();
    if (textSyleRec === null) return;
    console.log(textSyleRec);
    console.log("textSyleRec.fileName:" + textSyleRec.fileName);
    console.log("textSyleRec.name:" + textSyleRec.name);
});

Delete text style

After we get the target text style sheet record object McDbTextStyleTableRecord(), we can call the erase() method of the object instance to delete the object.

ts
import { MxCpp } from "mxcad"

let textSyleTable = MxCpp.getCurrentMxCAD().getDatabase().getTextStyleTable()
let textStyleId = textSyleTable.get("Target text style name")
textStyleId.erase()
// Update display
mxcad.updateDisplay()

Tip

Before deleting a text style, it is a good idea to determine whether a text object on the diagram is using the text style.

Change the text style

The basic operation of modifying the text style in mxcad is to get the text style sheet, and then get the text style sheet record object according to the text style name or text style ID, and set the corresponding attribute value of the text style sheet record object. Let the user select a text object on the CAD drawing, and then manipulate the text style applied to the object as an example:

ts
import { MxCADSelectionSet, MxCADUiPrPoint, MxCADResbuf } from "mxcad"

const ss = new MxCADSelectionSet();
const getPoint = new MxCADUiPrPoint()
const filter = new MxCADResbuf();
// Add object types, select sets Select only objects of literal type
filter.AddMcDbEntityTypes("TEXT,MTEXT");
getPoint.setMessage("Please select the target object")
const point = await getPoint.go()
if (!point) return;
const index = ss.pointSelect(point.x, point.y,filter)
const ent = ss.item(index).getMcDbEntity()
if(!ent) return
let textStyleId = ent.textStyleId
let textStyleRec = textStyleId.getMcDbTextStyleTableRecord()
console.log(textStyleRec)
// Change the font height of the text style
textStyleRec.textSize = 10
// Change the text width scaling ratio
textStyleRec.xScale = 0.7
// Change the text tilt Angle
textStyleRec.obliquingAngle = 0.3

Modify the text style of the text object

Text objects all have a textStyle property, which you can use to set or take the text style of the text object. The following is an example of setting the text style of a text object to Song typeface.

ts
// Modify the object text style
import { MxCpp } from "mxcad";

let mxcad = MxCpp.getCurrentMxCAD();
// Clear the current display
mxcad.newFile();
MxCpp.App.loadFonts([], [], ["syadobe","simsun"], () => {
    // Add a boldface text style
    mxcad.AddTureTypeTextStyle("ht_style","syadobe");
    // Add a typeface text style
    mxcad.AddTureTypeTextStyle("st_style","simsun");
    // Set the current style to bold
    mxcad.drawTextStyle = "ht_style";
    mxcad.drawColor = new McCmColor(200, 255, 50);
    let idText = mxcad.drawText(0, 3500, "Drawing control TrueType text test ", 100, 0, 0, 1);
    let ent = idText.getMcDbEntity();
    // Change the text style to Song typeface
    if(ent) ent.textStyle = "st_style";
    mxcad.zoomAll();
    mxcad.regen();
    mxcad.updateDisplay();
});

Get the font file

We can call the fileName attribute in McDbTextStyleTableRecord to get the name of the Western font, and call the bigFontFileName attribute to get the name of the Chinese font. Here we take the font file used by all the text objects in the diagram as an example, and obtain the font information used by all the CAD text objects on the diagram.

tap McDbTextStyleTableRecord.fileName, McDbTextStyleTableRecord.bigFontFileName View detailed attribute and method descriptions.

ts
import { MxCpp, MxCADSelectionSet } from "mxcad";

const mxcad = MxCpp.getCurrentMxCAD();
// Get the data table
const dataBase = mxcad.getDatabase();
// Returns a literal stylesheet table object in the database
const textStyleTable = dataBase.getTextStyleTable();
// Define a selection set object
const ss = new MxCADSelectionSet();
// Select all objects on the graph.
ss.allSelect();
// Go through all objects
for(let i = 0; i<ss.count();i++){
    const ent = ss.item(i).getMcDbEntity();
    // Gets a record of the text style used by the object
    const rec = textStyleTable.get(ent.textStyle).getMcDbTextStyleTableRecord();
    if(rec === null) continue;
    // Get the Western font
    const sShxFileName = rec.fileName;
    // Get the Chinese font
    const sBigShxFileName = rec.bigFontFileName;
    console.log(" Western font ", sShxFileName)
    console.log(" Chinese font ", sBigShxFileName)  
}