在上一个教程中,我们介绍了一个简单的ABD命令,使用C++代码实现。这一节我们介绍使用C#代码实现同样功能的命令。
主要功能和上一个教程相同,这里略过。
在硬盘D盘上建一个目录,名字是helloabd.net。使用文本编辑器,生成几个文件,文件名和内容分别是:
DemoSrcDir = $(_MakeFilePath)
PolicyFile = MicroStationPolicy.mki
appName = HelloABD.NET
MDLMKI = $(MSMDE)mki/
#--------------------------------------------------------------------------------------------------
# Includes
#--------------------------------------------------------------------------------------------------
%include $(MDLMKI)mdl.mki
%if $(TARGET_PROCESSOR_ARCHITECTURE)=="x64"
Platform = x64
OutSubDir = Winx64
%else
Platform = x86
OutSubDir = Winx86
%endif
o = $(HelloABD.NETBuildDir_)
%if $(TARGET_PROCESSOR_ARCHITECTURE)=="x64"
OutSubDir = Winx64
%else
OutSubDir = Winx86
%endif
always:
| ************************************************
| * SlnFile = $(SlnFile)
| ************************************************
buildConfiguration = "$(Configuration)|$(Platform)"
always:
| ************************************************
| * Processing HelloABD.NET ...
| * BuildConfiguration=$(buildConfiguration)
| * ProductDir= $(MS)
| ************************************************
#--------------------------------------------------------------------------------------------------
# Compile
#--------------------------------------------------------------------------------------------------
%if defined(BMAKE_DELETE_ALL_TARGETS)
Clean:
devenv "$(SlnFile)" \/clean $(buildConfiguration)
%else
%if defined (BMAKE_BUILD_ALL)
BuildOp = \/rebuild
%else
BuildOp = \/build
%endif
BuildOrRebuild:
devenv "$(SlnFile)" $(BuildOp) $(buildConfiguration) # $(UseEnvOpt)
%endif
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4ED575D9-25FB-419A-8B94-068F7A195825}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HelloABD.NET</RootNamespace>
<AssemblyName>HelloABD.NET</AssemblyName>
<FileAlignment>512</FileAlignment>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>$(MS)mdlapps\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>$(MS)mdlapps\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>$(MS)mdlapps\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>false</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>$(MS)mdlapps\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>
$(MS)mdlapps\
</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>false</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>
$(MS)mdlapps\
</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisFailOnMissingRules>false</CodeAnalysisFailOnMissingRules>
</PropertyGroup>
<PropertyGroup>
<RunPostBuildEvent>Always</RunPostBuildEvent>
</PropertyGroup>
<ItemGroup>
<Reference Include="Bentley.Building.Api">
<HintPath>$(MS)Assemblies\Bentley.Building.Api.dll</HintPath>
</Reference>
<Reference Include="Bentley.DgnDisplayNet">
<HintPath>$(MS)Bentley.DgnDisplayNet.dll</HintPath>
<private>False</private>
</Reference>
<Reference Include="Bentley.DgnPlatformNET">
<HintPath>$(MS)Bentley.DgnPlatformNET.dll</HintPath>
<private>False</private>
</Reference>
<Reference Include="Bentley.ECObjects3">
<HintPath>$(MS)Assemblies\ECFramework\Bentley.ECObjects3.dll</HintPath>
<private>False</private>
</Reference>
<Reference Include="Bentley.General.1.0">
<HintPath>$(MS)Assemblies\ECFramework\Bentley.General.1.0.dll</HintPath>
<private>False</private>
</Reference>
<Reference Include="Bentley.GeometryNET">
<HintPath>$(MS)Bentley.GeometryNET.dll</HintPath>
<private>False</private>
</Reference>
<Reference Include="Bentley.GeometryNET.Common">
<HintPath>$(MS)Bentley.GeometryNET.Common.dll</HintPath>
<private>False</private>
</Reference>
<Reference Include="Bentley.GeometryNET.Structs">
<HintPath>$(MS)Bentley.GeometryNET.Structs.dll</HintPath>
<private>False</private>
</Reference>
<Reference Include="Bentley.Interop.ATFCom">
<HintPath>$(MS)Assemblies\Bentley.Interop.ATFCom.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Bentley.Interop.MicroStationDGN">
<HintPath>$(MS)Assemblies\Bentley.Interop.MicroStationDGN.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Bentley.Interop.STFCom">
<HintPath>$(MS)Assemblies\Bentley.Interop.STFCom.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Bentley.Interop.TFCom">
<HintPath>$(MS)Assemblies\Bentley.Interop.TFCom.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Bentley.MicroStation.Interfaces.1.0">
<HintPath>$(MS)Assemblies\Bentley.MicroStation.Interfaces.1.0.dll</HintPath>
<private>False</private>
</Reference>
<Reference Include="Bentley.Platform">
<HintPath>$(MS)Assemblies\ECFramework\Bentley.Platform.dll</HintPath>
<private>False</private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="ustation">
<HintPath>$(MS)ustation.dll</HintPath>
<private>False</private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="HelloABD.NET.cs" />
<Compile Include="AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="build.bat" />
<None Include="HelloABD.NET.mke" />
<None Include="openSln.bat" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="commands.xml">
<LogicalName>CommandTable.xml</LogicalName>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>rd /s /q "$(SolutionDir)obj"</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{E1F84DFD-1DB1-4B2A-A250-FC6642C30058}") = "HelloABD.NET", "HelloABD.NET.csproj", "{4ED575D9-25FB-419A-8B94-068F7A195825}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4ED575D9-25FB-419A-8B94-068F7A195825}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4ED575D9-25FB-419A-8B94-068F7A195825}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4ED575D9-25FB-419A-8B94-068F7A195825}.Debug|x64.ActiveCfg = Debug|x64
{4ED575D9-25FB-419A-8B94-068F7A195825}.Debug|x64.Build.0 = Debug|x64
{4ED575D9-25FB-419A-8B94-068F7A195825}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4ED575D9-25FB-419A-8B94-068F7A195825}.Release|Any CPU.Build.0 = Release|Any CPU
{4ED575D9-25FB-419A-8B94-068F7A195825}.Release|x64.ActiveCfg = Release|x64
{4ED575D9-25FB-419A-8B94-068F7A195825}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.GeometryNET;
using Bentley.Building.Api;
namespace HelloABD.NET
{
class PlaceWallTool : DgnPrimitiveTool
{
public void CreateWall(DPoint3d start)
{
start.Y = start.Z = 0;
DPoint3d opposite = new DPoint3d(start.X, 5000, 0);
TFFormRecipeLinearList form = new TFFormRecipeLinearList();
Bentley.DgnPlatformNET.DgnModelRef modelRef = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModelRef();
form.Init("");
form.SetModelRef(modelRef, 0);
ITFFormRecipeLinear wallForm = form.AsTFFormRecipeLinear;
wallForm.SetEndPoints(ref start, ref opposite, 0);
wallForm.SetThickness(200, 0);
wallForm.SetTopFixedHeight(1000, 0);
string FamilyName = "WallComponent";
string PartName = "Brick";
ITFFormRecipe f = form.AsTFFormRecipe;
f.SetPartName(PartName, 0);
f.SetPartFamilyName(FamilyName, 0);
f.Synchronize(0);
Element elm;
f.GetElement(0, out elm);
string DGType = "Wall";
string DGItem = "Brick";
TFCatalogList datagroup = new TFCatalogList();
ITFCatalog catalog = datagroup.AsTFCatalog;
ITFCatalogItemList itemList;
catalog.GetCatalogItemByNames(DGType, DGItem, 0, out itemList);
ITFCatalogItem item = itemList.AsTFCatalogItem;
ITFPropertyList plist;
item.GetPropertyList(0, out plist);
if (plist != null)
{
ITFProperty tfProperty = plist.AsTFProperty;
if (tfProperty != null)
{
tfProperty.SetName("ObjectIdentity/@Description", 0);
ITFPropertyValue tfValue;
tfProperty.GetValue(0, out tfValue);
if (tfValue != null)
{
tfValue.SetWStringValue("Bentley Wall Test", 0);
tfProperty.SetValue(tfValue, 0);
item.SetProperty(tfProperty, 0);
}
}
}
item.AttachLinkagesOnElement(ref elm, 0);
elm.AddToModel();
}
public PlaceWallTool(int toolId, int prompt) : base(toolId, prompt)
{
}
protected override void OnRestartTool()
{
InstallNewInstance();
}
protected override void OnCleanup()
{
}
protected override void OnPostInstall()
{
Bentley.DgnPlatformNET.AccuSnap.SnapEnabled = true;
base.OnPostInstall();
}
protected override bool OnDataButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
{
CreateWall(ev.Point);
base.OnReinitialize();
return true;
}
protected override bool OnResetButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
{
ExitTool();
return true;
}
public static void InstallNewInstance()
{
PlaceWallTool wallTool = new PlaceWallTool(0, 0);
wallTool.InstallTool();
}
}
public sealed class CmdInterface
{
public static void PlaceWall(string unparsed)
{
HelloABDNET.Instance().StartPlaceWallTool();
}
}
[Bentley.MstnPlatformNET.AddInAttribute(MdlTaskID = "HelloABD.NET")]
public sealed class HelloABDNET : Bentley.MstnPlatformNET.AddIn
{
private static HelloABDNET s_Addin = null;
/*---------------------------------------------------------------------------------**//**
* Constructor.
/*--------------+---------------+---------------+---------------+---------------+------*/
public HelloABDNET(System.IntPtr mdlDesc)
: base(mdlDesc)
{
s_Addin = this;
}
/*---------------------------------------------------------------------------------**//**
* Required Run method of AddIn class.
/*--------------+---------------+---------------+---------------+---------------+------*/
protected override int Run(string[] commandLine)
{
return 0;
}
/*---------------------------------------------------------------------------------**//**
* Returns an instance of the Addin class.
* @bsimethod Bentley Systems
/*--------------+---------------+---------------+---------------+---------------+------*/
internal static HelloABDNET Instance()
{
return s_Addin;
}
internal void StartPlaceWallTool()
{
PlaceWallTool.InstallNewInstance();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<KeyinTree xmlns="http://www.bentley.com/schemas/1.0/MicroStation/AddIn/KeyinTree.xsd">
<RootKeyinTable ID="root">
<Keyword CommandWord="HELLOABDNET">
<Options Required="true" />
</Keyword>
</RootKeyinTable>
<KeyinHandlers>
<KeyinHandler Keyin="HELLOABDNET" Function="HelloABD.NET.CmdInterface.PlaceWall"/>
</KeyinHandlers>
</KeyinTree>
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HelloABD.NET")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HelloABD.NET")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("81EE0E38-4EB5-459C-946E-9CEF50F9D356")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@echo off @echo This batch file should be run from a VS2013 x64 Cross Tools Command Prompt @echo Make sure that you have already run ABDDeveloperShell.bat in @echo order to set the required environment variables. IF DEFINED MSMDE GOTO checkforvs2013 ECHO MSMDE not defined. You need to run ABDDeveloperShell.bat first! GOTO end :checkforvs2013 IF DEFINED vs120comntools GOTO vardefined ECHO Visual Studio 2013 is required for this project GOTO end :vardefined rem At this point "vcvarsall.bat x86_amd64" or VS2013 x64 Cross Tools Command Prompt should already have been called rem in order to compile successfully rem -------------------------------------- rem build the project in the sln rem -------------------------------------- @echo devenv.exe -useenv HelloABD.NET.sln /build devenv.exe -useenv HelloABD.NET.sln /build :end
@echo off @echo This batch file should be run from a VS2013 x64 Cross Tools Command Prompt @echo Make sure that you have already run ABDDeveloperShell.bat in @echo order to set the required environment variables. IF DEFINED MSMDE GOTO checkforvs2013 ECHO MSMDE not defined. You need to run ABDDeveloperShell.bat first! GOTO end :checkforvs2013 IF DEFINED vs120comntools GOTO vardefined ECHO Visual Studio 2013 is required for this project GOTO end :vardefined rem At this point "vcvarsall.bat x86_amd64" or VS2013 x64 Cross Tools Command Prompt should already have been called rem in order to compile successfully @echo devenv.exe -useenv HelloABD.NET.sln start devenv.exe -useenv HelloABD.NET.sln :end
打开桌面的ABD SDK快捷方式,在打开的Windows 命令行窗口中,输入命令
cd d:\helloabd.net build.bat
编译成功
下面使用使用命令行来打开Visual Studio 2015打开工程文件。
openSln.bat
调试代码的方式和上一节介绍的相同。