2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Block is a very important knowledge point in CAD. Today I will classify and introduce some blocks, as well as define and reference them.
A block is a whole defined by multiple logically related graphic objects. The advantage is that it can unify the work standards and improve the work efficiency, because objects in life are not simply a unit, but may be composed of multiple units. Blocks can well represent this structure.
The essence of a block is a collection of entity objects stored in a block table record.
Attribute blocks are composed of entities and additional information (attributes). The definition of blocks in attribute blocks is the same as that of simple blocks, while the definition of attributes is mainly realized through the relevant properties and functions of the attribute's AttributeDefinition class. The specific implementations are:
Among them, the attribute values of the attribute definition mainly include:
Dynamic blocks allow us to create blocks with high flexibility and customizability. Because once a traditional block is inserted into a drawing, its size, shape and position are usually fixed. Dynamic blocks are different. They can adjust their geometric properties through user interface interactions (such as dragging action handles) without redefining the entire block or manually editing its components.
Blocks are mainly divided into simple blocks, attribute blocks, and dynamic blocks. For blocks, there are mainly block definition operations, block reference operations (that is, block insertion operations), and block editing.
Regardless of the block, their definitions are roughly the same (attribute blocks require adding attribute objects). The specific steps are as follows:
The specific code is as follows:
public static ObjectId AddBlockThroughDB(this Database db, string blockName, List<Entity> ents)
{
//声明ObjectId,用于返回
ObjectId entId = ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForWrite);
BlockTableRecord btr = new BlockTableRecord();
btr.Name = blockName;
for (int ii = 0; ii < ents.Count; ii++)
{
Entity ent = ents[ii];
btr.AppendEntity(ent);
}
entId = bt.Add(btr);
tr.AddNewlyCreatedDBObject(btr, true);
tr.Commit();
}
return entId;
}
The above is just a simple block definition. As for the attribute block, as the name implies, you need to define the attribute fields in the block (we can understand it as defining attribute variables, which need to be converted into specific variable values when referenced).
Attribute block definition, we can add attributes to it by adding attributes.
/// <summary>
/// 块添加属性
/// </summary>
/// <param name="blockId"></param>
/// <param name="atts"></param>
public static void AddAttsToBlocks(this ObjectId blockId, List<AttributeDefinition> atts)
{
Database db = blockId.Database;//获取数据库对象
BlockTableRecord btr = blockId.GetObject(OpenMode.ForWrite) as BlockTableRecord;
foreach (AttributeDefinition att in atts)
{
btr.AppendEntity(att);
db.TransactionManager.AddNewlyCreatedDBObject(att, true);
}
btr.DowngradeOpen();
}
public static void AddAttsToBlocks(this ObjectId blockId, params AttributeDefinition[] atts)
{
blockId.AddAttsToBlocks(atts.ToList());
}
Add attributes to the specified block definition. We can use it like this:
//属性添加
AttributeDefinition attr1 = new AttributeDefinition(Point3d.Origin, "箱变", "箱变编号", "请输入箱变编号", ObjectId.Null);
AttributeDefinition attr2 = new AttributeDefinition(Point3d.Origin + new Vector3d(0, 0.25, 0),
"变电站", "变电站编号", "请输入变电站编号", ObjectId.Null);
//然后将这两个属性加入到集合,再调用上面的方法即可。
The parameters are described as follows:
Dynamic block definition is based on the use of attribute blocksDynamicBlockReferencePropertyCollection
Classes impose constraints on attributes.
public void Insert()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
BlockTableRecord space = db.CurrentSpaceId.GetObject(OpenMode.ForWrite) as BlockTableRecord;
//判断名为“块1”的块是否存在
if(! bt["块1"].IsNull)
{
BlockReference br = new BlockReference(Point3d.Origin, bt["块1"]);
br.ScaleFactors = new Scale3d(2.0);//设置尺寸为原来2倍
space.AppendEntity(br);
trans.AddNewlyCreatedDBObject(br, true);
trans.Commit();
}
else
{
return;
}
}
}
The attribute entity in the block reference is represented by the AttibuteReference class in the DatabaseServices namespace. It is actually a single-line text object derived from the DBText class. The steps to add attributes to a block reference are as follows:
/// <summary>
/// 插入带属性的参照快
/// </summary>
/// <param name="spaceId">空间的ID</param>
/// <param name="layer">块要加入的图层名</param>
/// <param name="blockName">快参照所属的快名</param>
/// <param name="postion">插入点</param>
/// <param name="scale">缩放比例</param>
/// <param name="rotateAngle">旋转角度</param>
/// <param name="attNameValues">属性名称与取值</param>
/// <returns></returns>
public static ObjectId InsertBlockrefence(this ObjectId spaceId, string layer, string blockName, Point3d postion, Scale3d scale, double rotateAngle, Dictionary<string, string> attNameValues)
{
// 获取数据库对象
Database db = spaceId.Database;
//以读的方式打开块表
BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
//如果没有blockName d的块,则程序返回
if (!bt.Has(blockName))
return ObjectId.Null;//如果没有blockName的块,程序返回
//以写的模式打开空间
BlockTableRecord space = (BlockTableRecord)spaceId.GetObject(OpenMode.ForWrite);
//获取块表的记录ID
ObjectId btrId = bt[blockName];
//打开块表记录
BlockTableRecord record = btrId.GetObject(OpenMode.ForRead) as BlockTableRecord;
//创建一个快参照并设置插入点
BlockReference br = new BlockReference(postion, bt[blockName]);
br.ScaleFactors = scale;
br.Layer = layer;
br.Rotation = rotateAngle;
space.AppendEntity(br);
//判断块表记录是否包含属性定义
if (record.HasAttributeDefinitions)
{
//若包含,则遍历属性定义
foreach (ObjectId id in record)
{
//检查是否是属性定义
AttributeDefinition attDef = id.GetObject(OpenMode.ForRead) as AttributeDefinition;
if (attDef != null)
{
//创建一个新的属性对象
AttributeReference attribute = new AttributeReference();
//从属性定义获取属性对象的对象特性
attribute.SetAttributeFromBlock(attDef, br.BlockTransform);
attribute.Rotation = attDef.Rotation;
attribute.Position = attDef.Position.TransformBy(br.BlockTransform);
attribute.AdjustAlignment(db);
//判断是否包含指定的属性名称
if (attNameValues.ContainsKey(attDef.Tag.ToUpper()))
{
//设置属性值
attribute.TextString = attNameValues[attDef.Tag.ToUpper()].ToString();
}
// 向块参照添加属性对象
br.AttributeCollection.AppendAttribute(attribute);
db.TransactionManager.AddNewlyCreatedDBObject(attribute, true);
}
}
}
db.TransactionManager.AddNewlyCreatedDBObject(br, true);
return br.ObjectId;//返回添加的快参照的ID
}
Some of the above parameters may not be necessary, such as the scaling and insertion ratio.
/// <summary>
/// 更新属性名称与取值
/// </summary>
/// <param name="blockRefId"></param>
/// <param name="attNameValues"></param>
public static void UpdateAttributesInBlock(this ObjectId blockRefId, Dictionary<string, string> attNameValues)
{
BlockReference blockRef = blockRefId.GetObject(OpenMode.ForRead) as BlockReference;
if (blockRef != null)
{
foreach (ObjectId id in blockRef.AttributeCollection)
{
AttributeReference attref = id.GetObject(OpenMode.ForRead) as AttributeReference;
if (attNameValues.ContainsKey(attref.Tag.ToUpper()))
{
attref.UpgradeOpen();
//设置属性值
attref.TextString = attNameValues[attref.Tag.ToUpper()].ToString();
attref.DowngradeOpen();
}
}
}
}