Working with Blocks
Working with Blocks
Blocks in CadSDK provide a powerful way to create reusable groups of elements. A block consists of two main components:
- A BlockDefinition that serves as a template, containing the elements that make up the block
- One or more BlockReference instances that place the block in the drawing with specific positions, rotations, and scales
See BlockReference in the Elements section for the complete class definition.
Basic Block Example
// Create a block definition
var blockDef = new BlockDefinition()
{
Name = "SimpleBlock",
ReferencePoint = Vector3.Zero,
LengthUnit = LengthUnit.Millimeters
};
// Add the block definition to the drawing session
session.AddBlock(blockDef);
// Add elements to the block definition
var lineInBlock = new Line(new Vector3(-5, -5, 0), new Vector3(5, 5, 0))
{
ColorMethod = PropertyMethod.Custom,
ColorARGB = AciColors.White
};
var circleInBlock = new Circle(Vector3.Zero, 5f)
{
ColorMethod = PropertyMethod.ByBlock
};
blockDef.AddElement(lineInBlock);
blockDef.AddElement(circleInBlock);
// Create a block reference
var blockRef = new BlockReference(
blockDef.Name,
new Vector3(100, 0, 0), // Insertion point
MathF.PI / 4, // 45-degree rotation
new Vector3(2, 2, 1) // Scale
);
// Add the block reference to the drawing session
session.AddElement(blockRef);
# Coming soon...
Block with Attributes Example
// Create a block definition with an attribute
var blockDef = new BlockDefinition()
{
Name = "BlockWithAttribute",
ReferencePoint = Vector3.Zero
};
// Add the block definition to the drawing session
session.AddBlock(blockDef);
// Add an attribute definition to the block
var attDef = new AttributeDefinition(
new Vector3(0, -10, 0), // Insertion point
"PART_NUMBER", // Tag
"Enter Part Number:", // Prompt
"PN-000", // Default value
2f // Text height
);
blockDef.AddElement(attDef);
// Create a block reference with an attribute value
var blockRef = new BlockReference(
blockDef.Name,
new Vector3(0, 0, 0)
);
blockRef.Attributes.Add("PART_NUMBER", "PN-123"); // Override default value
// Add the block reference to the drawing session
session.AddElement(blockRef);
# Coming soon...