Skip to content
On this page

Block

We can create a block by instantiating the McDbBlockReference() object. A block is a collection of multiple graphics into an object that can be moved, copied, or inserted into a graph multiple times as a whole. In mxcad can be combined with McDbBlockTableRecord() block record object to create a custom block, A block is defined by setting the block's base point origin, adding block record appendAcDbEntity(), and block position.

Click on the McDbBlockReference(), McDbBlockTableRecord() to check the detailed description attributes and methods.

ts
import { MxCpp, McDbBlockTableRecord, McDbBlockReference, McDbLine, McCmColor } from "mxcad"

let mxcad = MxCpp.getCurrentMxCAD();
// First get the block table from the database
let blkTable =  mxcad.getDatabase().getBlockTable();
// Adds a new block record to the block table
let blkRecId = blkTable.add(new McDbBlockTableRecord());

// Get the newly added block record again based on ObjectId
let blkTableRecord:McDbBlockTableRecord = blkRecId.getMcDbBlockTableRecord()

// Add two line segments and then the specific properties of each line segment here in the block record, such as the start point and the end point, are assigned to themselves
const line = new McDbLine(80, 80, 0, -80, -80, 0)
line.trueColor = new McCmColor(255, 0, 0)
const line1 = new McDbLine(-80, 80, 0, 80, -80, 0)
blkTableRecord.appendAcDbEntity(line);
blkTableRecord.appendAcDbEntity(line1);

// Set the base point of the block is generally a point in the surrounding box and can be specified arbitrarily
blkTableRecord.origin = new McGePoint3d(0,0,0);

// The instantiation block reference here needs to set the ObjectId we just added to the block record
let blkRef = new McDbBlockReference();
blkRef.blockTableRecordId = blkRecId;
// Finally set the position render block
blkRef.position = new McGePoint3d(0,0,0);

mxcad.drawEntity(blkRef);