标注C#

 

    //IFeatureWorkspaceAnno Example

    //The following example show how to use the CreateAnnotationClass on IFeatureWorkspaceAnno
    //to create a new feature-linked annotation class. In this function, the annotation class is
    //feature-linked to the feature class that is supplied as an argument and is created in the
    //same feature dataset as the feature class it is linked to.

    //The label expression is a simple expression of the values in the field called
    //DESCRIPTION in the feature class it is linked to. A text symbol for the feature-linked
    //annotation is red with a font of "Courier New".

    //e.g., nameOfFeatureClass = "States";
    //on ArcSDE use ISqlSyntax::QualifyTableName for fully qualified table names.
    public IFeatureClass IFeatureWorkspaceAnno_Example(IFeatureClass featureClass)
    {
        IDataset dataset = (IDataset)featureClass;
        //cast for the feature workspace from the workspace
        IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)dataset.Workspace;
        IFeatureWorkspaceAnno featureWorkspaceAnno = (IFeatureWorkspaceAnno)dataset.Workspace;
        //set up the reference scale
        ESRI.ArcGIS.Carto.IGraphicsLayerScale graphicLayerScale =
            new ESRI.ArcGIS.Carto.GraphicsLayerScaleClass();
        IGeoDataset geoDataset = (IGeoDataset)dataset;
        graphicLayerScale.Units = ESRI.ArcGIS.esriSystem.esriUnits.esriFeet;
        graphicLayerScale.ReferenceScale = 2000;
        //set up symbol collection
        ESRI.ArcGIS.Display.ISymbolCollection symbolCollection =
            new ESRI.ArcGIS.Display.SymbolCollectionClass();

        #region "MakeText"
        ESRI.ArcGIS.Display.IFormattedTextSymbol myTextSymbol =
            new ESRI.ArcGIS.Display.TextSymbolClass();
        //set the font for myTextSymbol
        stdole.IFontDisp myFont = new stdole.StdFontClass() as stdole.IFontDisp;
        myFont.Name = "Courier New";
        myFont.Size = 9;
        myTextSymbol.Font = myFont;
        //set the Color for myTextSymbol to be Dark Red
        ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
        rgbColor.Red = 150;
        rgbColor.Green = 0;
        rgbColor.Blue = 0;
        myTextSymbol.Color = (ESRI.ArcGIS.Display.IColor)rgbColor;
        //Set other properties for myTextSymbol
        myTextSymbol.Angle = 0;
        myTextSymbol.RightToLeft = false;
        myTextSymbol.VerticalAlignment = ESRI.ArcGIS.Display.esriTextVerticalAlignment.esriTVABaseline;
        myTextSymbol.HorizontalAlignment = ESRI.ArcGIS.Display.esriTextHorizontalAlignment.esriTHAFull;
        myTextSymbol.CharacterSpacing = 200;
        myTextSymbol.Case = ESRI.ArcGIS.Display.esriTextCase.esriTCNormal;
        #endregion

        symbolCollection.set_Symbol(0, (ESRI.ArcGIS.Display.ISymbol)myTextSymbol);
        //set up the annotation labeling properties including the expression
        ESRI.ArcGIS.Carto.IAnnotateLayerProperties annoProps = new
            ESRI.ArcGIS.Carto.LabelEngineLayerPropertiesClass();
        annoProps.FeatureLinked = true;
        annoProps.AddUnplacedToGraphicsContainer = false;
        annoProps.CreateUnplacedElements = true;
        annoProps.DisplayAnnotation = true;
        annoProps.UseOutput = true;

        ESRI.ArcGIS.Carto.ILabelEngineLayerProperties layerEngineLayerProps =
            (ESRI.ArcGIS.Carto.ILabelEngineLayerProperties)annoProps;
        ESRI.ArcGIS.Carto.IAnnotationExpressionEngine annoExpressionEngine =
            new ESRI.ArcGIS.Carto.AnnotationVBScriptEngineClass();
        layerEngineLayerProps.ExpressionParser = annoExpressionEngine;
        layerEngineLayerProps.Expression = "[DESCRIPTION]";
        layerEngineLayerProps.IsExpressionSimple = true;
        layerEngineLayerProps.Offset = 0;
        layerEngineLayerProps.SymbolID = 0;
        layerEngineLayerProps.Symbol = myTextSymbol;

        ESRI.ArcGIS.Carto.IAnnotateLayerTransformationProperties annoLayerTransProp =
            (ESRI.ArcGIS.Carto.IAnnotateLayerTransformationProperties)annoProps;
        annoLayerTransProp.ReferenceScale = graphicLayerScale.ReferenceScale;
        annoLayerTransProp.Units = graphicLayerScale.Units;
        annoLayerTransProp.ScaleRatio = 1;

        ESRI.ArcGIS.Carto.IAnnotateLayerPropertiesCollection annoPropsColl =
            new ESRI.ArcGIS.Carto.AnnotateLayerPropertiesCollectionClass();
        annoPropsColl.Add(annoProps);
        //use the AnnotationFeatureClassDescription to get the list of required
        //fields and the default name of the shape field
        IObjectClassDescription oCDesc = new ESRI.ArcGIS.Carto.AnnotationFeatureClassDescriptionClass();
        IFeatureClassDescription fCDesc = (IFeatureClassDescription)oCDesc;
       
        //create the new class
        return featureWorkspaceAnno.CreateAnnotationClass("AnnoTest",
            oCDesc.RequiredFields, oCDesc.InstanceCLSID, oCDesc.ClassExtensionCLSID,
            fCDesc.ShapeFieldName, "", featureClass.FeatureDataset, featureClass,
            annoPropsColl, graphicLayerScale, symbolCollection, true);
    }

<think>嗯,用户想了解如何在CAD二次开发中使用C#创建水平标注。首先,我需要回忆一下AutoCAD的.NET API相关知识点。记得在CAD二次开发中,标注对象通常属于DatabaseServices命名空间,可能需要用到Dimension类的子类,比如AlignedDimension或者RotatedDimension。 用户提到的是水平标注,可能需要指定旋转角度为0。AlignedDimension通常是平行于两个定义点的,而如果是要水平方向的,可能需要使用RotatedDimension,并设置旋转角度为0度。不过需要确认一下具体用哪个类更合适。 接下来,步骤应该是:获取当前数据库、开启事务、创建标注对象、设置属性(如尺寸线位置、文字位置、旋转角度等),然后将标注添加到模型空间。同时,用户可能需要示例代码,所以得确保代码语法正确,并符合C#的规范。 参考之前的引用,比如引用[3]中提到了创建线性标注对象,设置标注文本。这可能和水平标注类似。需要确认创建标注时的参数是否正确,比如使用点坐标时是否正确,是否需要考虑UCS(用户坐标系)的问题,但可能在示例中暂时忽略,使用WCS(世界坐标系)简化。 另外,要确保代码中包含必要的命名空间,如Autodesk.AutoCAD.ApplicationServices,DatabaseServices,Runtime等。事务处理也是关键,使用Transaction来管理数据库操作。 可能需要分步骤说明:1. 添加引用和命名空间;2. 创建标注的方法;3. 定义起点和终点;4. 设置标注属性;5. 添加标注到模型空间。同时,要注意旋转角度设为0度,确保标注是水平的。 然后,检查是否有错误,例如点坐标是否正确,是否需要使用BlockTable和BlockTableRecord来添加实体。在引用[3]的示例中,用户直接将标注添加到模型空间,所以可能需要使用当前空间。例如,使用blockTable[BlockTableRecord.ModelSpace]来打开模型空间块表记录。 最后,生成相关问题,比如如何修改标注样式,如何处理用户坐标系下的标注,或者如何动态交互选择点。这些问题可以帮助用户进一步学习相关内容。</think>在AutoCAD二次开发中,使用C#创建水平标注主要涉及`AlignedDimension`或`RotatedDimension`类。以下是具体实现步骤和示例代码: ### 一、创建水平标注步骤 1. **设置标注参数**:定义标注起点、终点和尺寸线位置 2. **创建标注对象**:使用`RotatedDimension`并设置旋转角度为0 3. **配置标注样式**:设置文字高度、偏移量等属性 4. **添加标注到模型空间**:通过事务处理完成数据库操作 ### 二、示例代码 ```csharp using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; public class HorizontalDimensionCreator { [CommandMethod("CreateHDim")] public void CreateHorizontalDimension() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; using (Transaction tr = db.TransactionManager.StartTransaction()) { // 定义标注参数 Point3d startPoint = new Point3d(0, 0, 0); Point3d endPoint = new Point3d(10, 0, 0); Point3d dimLinePos = new Point3d(5, 2, 0); // 创建水平标注(旋转角度0度) RotatedDimension dim = new RotatedDimension( startPoint, endPoint, dimLinePos, "5", // 标注文字 db.Dimstyle, // 当前标注样式 0 // 旋转角度 ); // 设置标注属性 dim.Dimtxsty = db.Textstyle; // 文字样式 dim.Dimscale = 1; // 比例因子 dim.Dimtad = 1; // 文字垂直位置 // 添加标注到模型空间 BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; btr.AppendEntity(dim); tr.AddNewlyCreatedDBObject(dim, true); tr.Commit(); } } } ``` ### 三、关键参数说明 1. **旋转角度**:设置为0表示水平方向 2. **DimLinePos**:尺寸线位置需位于标注方向垂直偏移位置 3. **Dimtad**:控制文字相对于尺寸线的垂直位置[^3]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值