Block Reference Example
This example demonstrates creating and using block references with layer-based colors. It shows how to define blocks, add elements to them, and create references with attributes.
using CadSDK;
using CadSDK.Elements;
using CadSDK.Enums;
using CadSDK.Geometry;
using System.Numerics;
// Create a drawing session
var session = new DrawingSession
{
ExportFormat = ExportFormat.DWG,
CadVersion = CadVersion.Cad2018
};
// Define layers
var blockLayer = new Layer { Name = "TestLayer_Blocks", ColorARGB = AciColors.Yellow };
var geoLayer = new Layer { Name = "TestLayer_BlockGeometry", ColorARGB = AciColors.GrayishCrimson2 };
session.AddLayer(blockLayer);
session.AddLayer(geoLayer);
// Block definition demonstrating layer colors
var blockDef1 = new BlockDefinition()
{
Name = "CadSDK_Test_Block_001",
ReferencePoint = Vector3.Zero,
LengthUnit = LengthUnit.Millimeters
};
session.AddBlock(blockDef1);
// Elements in block using layer colors and ByBlock
var line1 = new Line(new Vector3(-5, -5, 0), new Vector3(5, 5, 0))
{
LayerName = geoLayer.Name
};
var circle1 = new Circle(Plane3D.WorldXY, Vector3.Zero, 5f)
{
ColorMethod = PropertyMethod.ByBlock
};
blockDef1.AddElement(line1);
blockDef1.AddElement(circle1);
var attDef1 = new AttributeDefinition(new Vector3(0, -10, 0), "PART_NUMBER", "Enter Part Number:", "PN-000", 2f, Plane3D.WorldXY)
{
LayerName = geoLayer.Name
};
blockDef1.AddElement(attDef1);
// Block reference using layer color
var blockRef1 = new BlockReference(blockDef1.Name, new Vector3(100, 0, 0))
{
LayerName = blockLayer.Name
};
blockRef1.Attributes.Add("PART_NUMBER", "PN-123");
session.AddElement(blockRef1);
// Export the drawing
// You can signup and get your free API key
string apiKey = "your-api-key"; // Get your free API key from the dashboard
string outputPath = "drawing.dwg";
await session.ExportAndSaveAsync(apiKey, outputPath);
# Coming soon...
Key Points
-
Block references allow reuse of geometry:
- First define a
BlockDefinition
containing elements - Then create
BlockReference
instances to place it in the drawing
- First define a
-
Block elements can use different color methods:
PropertyMethod.ByLayer
: Inherit color from layerPropertyMethod.ByBlock
: Inherit color from block referencePropertyMethod.Custom
: Use own color
-
Attributes allow for variable text in blocks:
- Define attributes using
AttributeDefinition
in block definition - Set values using
Attributes
dictionary in block reference
- Define attributes using
- Each block definition needs a unique name and reference point.
- Block references can be placed at different positions with their own properties.