从AutoCAD2006开始引入了动态块这个概念,动态块的使用使得我们的图形更加灵活,可以通过自定义夹点或自定义特性来操作几何图形。
这使得我们可以根据需要在位调整块参照,而不用搜索另一个块以插入或重定义现有的块。也就是可以通过一个块定义衍生出多个特性类似的块参照。 目前用.NET环境开发还不能自定义约束、关联以及动态块,但是我们可以用.NET程序访问或者控制动态块的参数,看下面的例子修改动态块的参数,使得动态块的图形变化。
- using System;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.EditorInput;
- [assembly: CommandClass(typeof(Sample.SetDynamicBlockValue))]
- namespace Sample
- {
- class SetDynamicBlockValue
- {
- [CommandMethod("SetDynamicBlockValue")]
- public void SetValue()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockReference br = (BlockReference)SelectEntity("\n选择动态块");
- Property[] Properties = new Property[] { new Property("Width", 100), new Property("Height", 100) };
- SetDynamicValue(br, Properties);
- tr.Commit();
- }
- }
- /// <summary>
- /// 提示用户选择单个实体
- /// </summary>
- /// <param name="word">选择提示</param>
- /// <returns>实体对象</returns>
- public static Entity SelectEntity(string word)
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- Entity entity = null;
- PromptEntityResult ent = ed.GetEntity(word);
- if (ent.Status == PromptStatus.OK)
- {
- using (Transaction transaction = db.TransactionManager.StartTransaction())
- {
- entity = (Entity)transaction.GetObject(ent.ObjectId, OpenMode.ForWrite, true);
- transaction.Commit();
- }
- }
- return entity;
- }
- /// <summary>
- /// 设置动态块属性
- /// </summary>
- /// <param name="br">要设置属性的动态块</param>
- /// <param name="PropertyNames"></param>
- /// <param name="Values"></param>
- public static void SetDynamicValue(BlockReference br, Property[] Properties)
- {
- try
- {
- if (br.IsDynamicBlock)
- {
- foreach (DynamicBlockReferenceProperty dbrp in br.DynamicBlockReferencePropertyCollection)
- {
- for (int i = 0; i < Properties.Length; i++)
- {
- if (dbrp.PropertyName == Properties.PropertyName)
- {
- dbrp.Value = Properties.Value;
- }
- }
- }
- }
- }
- catch
- {
- }
- }
- }
- /// <summary>
- /// 动态块属性
- /// </summary>
- public class Property
- {
- private string propertyname;
- private double value;
- public string PropertyName
- {
- get { return propertyname; }
- }
- public double Value
- {
- get { return value; }
- }
- public Property(string PropertyName, double Value)
- {
- propertyname = PropertyName;
- value = Value;
- }
- }
- }
复制代码- <div align="left"><font color="#696969"><font face="Calibri"><font style="font-size: 10pt"></font></font></font></div><font face="Calibri"><font color="#696969"><font style="font-size: 10pt"><i><i><font face="Calibri"><font style="font-size: 10pt">加载程序运行命令后选择一个动态块:</font></font></i></i></font></font></font><i><i>
- <div align="left"><font color="#696969"><font face="Calibri"><font style="font-size: 13px"><font face="Calibri"><font style="font-size: 10pt">程序自动修改其属性参数,驱动动态块的形状改变:</font></font></font></font></font></div>
- <div align="center"><font color="#696969"></font></div><font color="#696969">
- <div align="left"><font color="#696969"><font face="Calibri"><font style="font-size: 13px"><font face="Calibri"><font style="font-size: 10pt"><font face="" "=""><font style="font-size: 10pt">*</font></font><font face="宋体"><font style="font-size: 10pt">在修改参数的时候需要注意的一点是所定义的约束参数必须是“增量”形式其值才能被任意修改,如果为“列表”形式则其值只能设定为列表中的值。</font></font></font></font></font></font></font></div></font></i></i>
复制代码
|