文字样式
创建文字样式
该方法用于创建自定义的文字样式,大致的流程为声明文字样式后设置文字样式中文字的字体,字体宽度,高度等属性。
public static void CreateTextStyle(string unparsed)
{
DgnFile dgnFile = Session.Instance.GetActiveDgnFile();//获得当前激活的dgn文件1
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获得声明文本元素的模型空间
double uorMeter = dgnModel.GetModelInfo().UorPerMeter;//获得当前模型中的单位
DgnTextStyle textStyle = new DgnTextStyle("TestTextStyle", dgnFile);//声明文本样式
DgnFont font = DgnFontManager.FindSystemFont("KaiTi",DgnFontFilterFlags.All);//获得名为"KaiTi"的字体
textStyle.SetFontProperty(TextStyleProperty.Font, font);//设置字体
textStyle.SetProperty(TextStyleProperty.Width,20* uorMeter);//设置文本宽度
textStyle.SetProperty(TextStyleProperty.Height, 20 * uorMeter);//设置文本高度
textStyle.Add(dgnFile);//将文本样式添加到文件中
#region Create text element
TextBlockProperties txtBlockProp = new TextBlockProperties(dgnModel);//声明文本属性
txtBlockProp.IsViewIndependent = false;//设置文本非独立于视图
ParagraphProperties paraProp = new ParagraphProperties(dgnModel);//声明段落属性
RunProperties runProp = new RunProperties(textStyle, dgnModel);//声明运行属性
TextBlock txtBlock = new TextBlock(txtBlockProp, paraProp, runProp, dgnModel);//声明文本块
txtBlock.AppendText("试验");//设置文本块文字内容
TextElement text = (TextElement)TextElement.CreateElement(null, txtBlock);//声明文本元素
text.AddToModel();//将生成的文本元素写入模型
#endregion
}
本案例中,首先创建了一个名为"TestTextStyle"的文字样式,并设置字体为楷体,文字宽度及高度均为20个单位长,然后创建文本块,设置文本内容后创建文本元素,最后将文本元素写入模型。
单元导入
参数化单元导入
参数化单元的导入方法为获得参数化单元存放地址后读取参数化文件中的信息,找到指定名称的模型。使用找到的模型在需要导入单元的文件中定义参数化单元定义元素,此时参数化单元即导入成功。
public static void ImportParametricCellElement(string unparsed)
{
const string cellName = "灯坑";//设置需要获取的单元名称
string filePath = "E:\\工作相关\\20211213MS案例直播\\直播资料\\Part4\\dengkeng.cel";//设置单元库路径
DgnDocument document = DgnDocument.CreateForLocalFile(filePath);//根据路径获取单元库文件
DgnFileOwner owner = DgnFile.Create(document,DgnFileOpenMode.ReadOnly);//声明可读取文件信息的DgnFile对象
DgnFile cellFile = owner.DgnFile;//获得读取文件信息的对象
cellFile.LoadDgnFile(out StatusInt fileLoadStatus);//读取文件信息
if(fileLoadStatus==StatusInt.Error)//若读取失败则返回
{
return;
}
ModelId id= cellFile.FindModelIdByName(cellName);//根据单元名称获取对应单元的模型ID
DgnModel cellModel = cellFile.LoadRootModelById(out StatusInt modelLoadStatus,id);//根据模型ID获取对应的模型空间
if (modelLoadStatus == StatusInt.Error)// 若读取失败则返回
{
return;
}
DgnFile dgnFile = Session.Instance.GetActiveDgnFile();//获取当前激活的文件
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前激活的模型
ParametricCellDefinitionElement pcDef = ParametricCellDefinitionElement.FindByName(cellName, dgnFile);//根据名称在文件中获取对应的参数化定义元素
if (null == pcDef)//判断是否存在指定的参数化模型定义
{
DgnComponentDefinitionHandler hdlr = DgnComponentDefinitionHandler.GetForModel(cellModel);//获得模型中的定义句柄
ParameterStatus status = hdlr.DefinitionModelHandler.CreateCellDefinition(dgnFile);//声明单元定义
if (ParameterStatus.Success == status)//判断是否声明成功
{
pcDef = ParametricCellDefinitionElement.FindByName(cellName, dgnFile);//根据名称在文件中获取对应的参数化定义元素
}
else
{
MessageCenter.Instance.ShowErrorMessage("Error Creating cellDef", null, true);//输出参数化单元定义声明失败的提示
return;
}
}
if (pcDef!=null)//判断参数化单元定义是否成功声明
{
ParametricCellElement pcElem = ParametricCellElement.Create(pcDef, null, dgnModel);//使用参数化定义声明参数化元素
DTransform3d trans = DTransform3d.Identity;//声明变换几何
TransformInfo transInfo = new TransformInfo(trans);//声明变换信息
pcElem.ApplyTransform(transInfo);//对参数化单元进行变换
pcElem.AddToModel();//将参数化单元元素写入模型
}
}
普通单元导入
与参数化单元导入的方法类似,大致的流程未首先将指定位置的单元模型文件读取到内存中,然后根据名称获取指定模型,读取模型中的元素,在目标模型中使用所得模型中的元素对普通单元进行定义。
public static void ImportCellElement(string unparsed)
{
const string cellName = "服务车道平缝加拉杆直径14mm";//设置需要获取的单元名称
string filePath = "E:\\工作相关\\20211213MS案例直播\\直播资料\\Part4\\拉杆 (1).cel";//设置单元库路径
DgnDocument document = DgnDocument.CreateForLocalFile(filePath);//根据路径获取单元库文件
DgnFileOwner owner = DgnFile.Create(document, DgnFileOpenMode.ReadOnly);//声明可读取文件信息的DgnFile对象
DgnFile cellFile = owner.DgnFile;//获得读取文件信息的对象
cellFile.LoadDgnFile(out StatusInt fileLoadStatus);//读取文件信息
if (fileLoadStatus == StatusInt.Error)//若读取失败则返回
{
return;
}
ModelId id = cellFile.FindModelIdByName(cellName);//根据单元名称获取对应单元的模型ID
DgnModel cellModel = cellFile.LoadRootModelById(out StatusInt modelLoadStatus, id);//根据模型ID获取对应的模型空间
if (modelLoadStatus == StatusInt.Error)// 若读取失败则返回
{
return;
}
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获取当前激活的模型
List<Element> elems = new List<Element>();//声明单元子元素列表
foreach (Element elem in cellModel.GetElements())//对普通单元的模型进行遍历
{
if (elem.IsGraphics)//将单元中的子元素添加到列表中
{
elems.Add(elem);
}
}
CellHeaderElement cellHeaderElem = CellHeaderElement.CreateOrphanCellElement(dgnModel, cellName, elems);//使用子元素声明普通单元
DTransform3d trans = DTransform3d.FromMatrixAndFixedPoint(DMatrix3d.Identity, DPoint3d.Zero);//声明几何变换
TransformInfo transform = new TransformInfo(trans);//声明变换信息
cellHeaderElem.ApplyTransform(transform);//对普通单元应用变换
cellHeaderElem.AddToModel();//将普通单元写入模型
}