Civil3D开发者指南系列之二——创建WPF面板、导出当前图形设置

1. 创建一个标签样式对象

所有的civil3D对象标签都由标签样式管理,称之为 LabelStyle 。一个标签样式可以包含多个文本标签,符号标记、直线、记号和箭头。
下面的实例将创建一个点的标签样式:

  1. public void CreatePointLabel()
  2. {
  3. var id = civilDoc.Styles.LabelStyles.PointLabelStyles.LabelStyles.Add("UCDBIM");
  4. }

2. 将命令集中在面板中

通过CivilTools命令调出面板

  1. [CommandMethod("CivilTools")]
  2. public void CivilTools()
  3. {
  4. var ps = new PaletteSet("CIVIL 3D工具集", new Guid("63B8DB5B-10E4-4924-B8A2-A9CF9158E4F6"));
  5. ps.Style = PaletteSetStyles.ShowPropertiesMenu | PaletteSetStyles.ShowAutoHideButton |
  6. PaletteSetStyles.ShowCloseButton;
  7. ps.MinimumSize = new System.Drawing.Size(800,450);
  8. var pl = new Palette();
  9. pl.Content= new CivilWindow();
  10. ps.AddVisual("工具集", pl);
  11. ps.Visible = true;
  12. }
  1. <UserControl x:Class="Civil3DAPI.CivilWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:Civil3DAPI"
  7. mc:Ignorable="d"
  8. d:DesignHeight="450" d:DesignWidth="800" Name="Civil_3D小工具"
  9. Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
  10. <Grid>
  11. <Grid.ColumnDefinitions>
  12. <ColumnDefinition />
  13. <ColumnDefinition />
  14. <ColumnDefinition />
  15. <ColumnDefinition />
  16. <ColumnDefinition />
  17. <ColumnDefinition />
  18. </Grid.ColumnDefinitions>
  19. <Grid.RowDefinitions>
  20. <RowDefinition />
  21. <RowDefinition />
  22. <RowDefinition />
  23. <RowDefinition />
  24. <RowDefinition />
  25. <RowDefinition />
  26. <RowDefinition />
  27. <RowDefinition />
  28. <RowDefinition />
  29. <RowDefinition />
  30. <RowDefinition />
  31. <RowDefinition />
  32. </Grid.RowDefinitions>
  33. <Button x:Name="bt00" Margin="8" Grid.Column="0" Click="Bt00_Click">创建点样式</Button>
  34. <Button x:Name="bt01" Margin="8" Grid.Column="0" Grid.Row="1" Click="Bt01_Click">获取线路设置</Button>
  35. <Button x:Name="bt02" Margin="8" Grid.Column="0" Grid.Row="2" Click="Bt02_Click">获取线路对象线型</Button>
  36. <Button x:Name="bt10" Margin="8" Grid.Column="1" Click="Button_Click">创建点标签样式</Button>
  37. </Grid>
  38. </UserControl>
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. namespace Civil3DAPI
  4. {
  5. /// <summary>
  6. /// CivilWindow.xaml 的交互逻辑
  7. /// </summary>
  8. public partial class CivilWindow : UserControl
  9. {
  10. public CivilWindow()
  11. {
  12. InitializeComponent();
  13. }
  14. private void Button_Click(object sender, RoutedEventArgs e)
  15. {
  16. var cs = new CivilSample();
  17. cs.CreatePointLabel();
  18. }
  19. private void Bt00_Click(object sender, RoutedEventArgs e)
  20. {
  21. var cs = new CivilSample();
  22. cs.CreatePointStyle("UCD");
  23. }
  24. private void Bt01_Click(object sender, RoutedEventArgs e)
  25. {
  26. var cs = new CivilSample();
  27. cs.AlignmentSetting();
  28. }
  29. private void Bt02_Click(object sender, RoutedEventArgs e)
  30. {
  31. var cs = new CivilSample();
  32. cs.AlignmentTypeOption();
  33. }
  34. }
  35. }

Civil3D开发者指南系列之二——创建WPF面板、导出当前图形设置 - 图1

3. 导出当前文件设置

  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using Autodesk.AutoCAD.EditorInput;
  7. using Autodesk.Civil.ApplicationServices;
  8. using Autodesk.Civil.Settings;
  9. using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  10. namespace Civil3DAPI
  11. {
  12. public class CSSample
  13. {
  14. public Editor ed;
  15. public string file;
  16. public StringBuilder xml;
  17. public void ExportCommandSettings()
  18. {
  19. var sfd = new SaveFileDialog();
  20. sfd.Filter = "XML 文件|*.xml";
  21. sfd.DefaultExt = "xml";
  22. sfd.FileName = "CMD_settings.xml";
  23. sfd.Title = "导出command settings";
  24. if (sfd.ShowDialog() == DialogResult.OK)
  25. file = sfd.FileName;
  26. else
  27. return;
  28. var civilDoc = CivilApplication.ActiveDocument;
  29. var doc = Application.DocumentManager.MdiActiveDocument;
  30. var ed = doc.Editor;
  31. var db = doc.Database;
  32. xml = new StringBuilder();
  33. xml.Append("<CommandSettings>\n");
  34. Type[] types =
  35. {
  36. typeof(SettingsRoot)
  37. };
  38. var aeccdbmgd = typeof(CivilDocument).Module.Assembly;
  39. var types2 = aeccdbmgd.GetTypes();
  40. foreach (var t in types2)
  41. if (t.Name.Contains("SettingsCmd")
  42. && (t.BaseType.Name.Contains("SettingsAmbient")
  43. || t.BaseType.BaseType.Name.Contains("SettingsAmbient")))
  44. {
  45. ed.WriteMessage("Command Setting found:" + t.Name + "\n");
  46. xml.Append(string.Format("<CommandSetting name=\"{0}\">\n", t.Name));
  47. try
  48. {
  49. var conInfo = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
  50. null, types, null);
  51. object[] s4 = {civilDoc.Settings};
  52. var settingsAmbient = (SettingsAmbient) conInfo.Invoke(s4);
  53. GetPropsOfObj(settingsAmbient, 2);
  54. }
  55. catch (Exception e)
  56. {
  57. ed.WriteMessage($"错误:\n{e.Message}\n{e.GetType()}");
  58. }
  59. xml.Append("</CommandSetting>\n");
  60. }
  61. xml.Append("</CommandSettings>\n");
  62. using (var sw = new StreamWriter(file))
  63. {
  64. sw.Write(xml.ToString());
  65. sw.Flush();
  66. sw.Close();
  67. }
  68. ed.WriteMessage("DONDE!\n");
  69. }
  70. private void GetPropsOfObj(object settingsAmbient, int v)
  71. {
  72. var space = " ".PadRight(v);
  73. var type = settingsAmbient.GetType();
  74. var properties = type.GetProperties(
  75. BindingFlags.Public | BindingFlags.Instance);
  76. foreach (var info in properties)
  77. {
  78. var obsoleteattri = info.GetCustomAttributes(
  79. typeof(ObsoleteAttribute), true);
  80. if (obsoleteattri.Length > 0) continue;
  81. object prop = null;
  82. try
  83. {
  84. prop = type.InvokeMember(info.Name, BindingFlags.GetProperty,
  85. null, settingsAmbient, new object[0]);
  86. }
  87. catch (Exception e)
  88. {
  89. ed.WriteMessage($"错误:{type.Name} {info.Name} {e.Message}\n{e.GetType()}\n");
  90. }
  91. if (prop.GetType().Name.Contains("Settings"))
  92. {
  93. xml.Append(string.Format(space + "<Property name=\"{0}\">\n", info.Name));
  94. GetPropsOfObj(prop, v + 3);
  95. xml.Append(space + "</Property>\n");
  96. }
  97. else if (prop.GetType().Name.Contains("Property"))
  98. {
  99. var propType = prop.GetType();
  100. try
  101. {
  102. var val = propType.InvokeMember("Value", BindingFlags.GetProperty, null, prop, new object[0]);
  103. var strvalue = val.ToString();
  104. if (strvalue.Contains("<") || strvalue.Contains(">"))
  105. strvalue = strvalue.Replace("<", "").Replace(">", "");
  106. xml.Append(string.Format(space + "<Property name=\"{0}\" value=\"{1}\"/>\n", info.Name,
  107. strvalue));
  108. }
  109. catch (Exception e)
  110. {
  111. ed.WriteMessage(
  112. $"错误:{type.Name} {info.Name} {propType.Name}Value {e.Message}\n{e.GetType()}\n");
  113. }
  114. }
  115. else
  116. {
  117. ed.WriteMessage("Didn't process:{0}{1}\n",
  118. info.Name, info.PropertyType.ToString());
  119. }
  120. }
  121. }
  122. }
  123. }

Civil3D开发者指南系列之二——创建WPF面板、导出当前图形设置 - 图2

Civil3D开发者指南系列之二——创建WPF面板、导出当前图形设置 - 图3