Draw Dimension feature using ArcObjects

403
0
12-07-2021 01:24 AM
MohammedAlsadaany
New Contributor II

Hello all

 

    I have a task I need to create a tool to draw a dimension feature using ArcObjects C#, I did many search but I can not find any think about this.

I tried to draw the dimension by cerate 3 point and then store the feature , the dimension created successfully but the dimension created with a wrong angle so the length the line becomes wrong.

Capture.JPG

 this image shows the difference between the dimension draw by my tool (the selected one in blue) and the right one.

and that is my code

 

 

 

 

 

 

 private IPoint pPoint1;
        private IPoint pPoint2;
        private IPoint pPoint3;
        private int i = 0;

 protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)
        {
            try
            {
                i++;

                string layerName = GlobalVariables.DimensionLayer;
                IMxDocument mxDoc = ArcMap.Application.Document as IMxDocument;
                IMap m_pMap;
                m_pMap = ArcMap.Document.FocusMap;
                if (i == 1)
                {
                    pPoint1 = mxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
                }
                else if (i == 2)
                {
                    pPoint2 = mxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
                }
                else if (i == 3)
                {
                    i = 0;
                    pPoint3 = mxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);

                    #region Dimension
                    IFeatureLayer pePipeFeatureLayer = null;
                    ILayer pePipeLayer = null;
                    for (int j = 0; j < m_pMap.LayerCount; j++)
                    {
                        pePipeLayer = m_pMap.Layer[j];
                        if (pePipeLayer.Name.Contains(layerName))
                        {
                            pePipeFeatureLayer = (IFeatureLayer)pePipeLayer;
                        }
                    }
                    IFeatureClass pFClass;
                    pFClass = pePipeFeatureLayer.FeatureClass;
                    IDataset pePipeDataset = (IDataset)pePipeFeatureLayer;

                    #endregion


                    IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)((IDataset)(pePipeFeatureLayer.FeatureClass)).Workspace;
                    workspaceEdit.StartEditing(false);
                    workspaceEdit.StartEditOperation();

                    IFeature pFeature = pePipeFeatureLayer.FeatureClass.CreateFeature();
                    int dimLength = pFeature.Fields.FindField("DIMLENGTH");
                    int BEGINX = pFeature.Fields.FindField("BEGINX");
                    int BEGINY = pFeature.Fields.FindField("BEGINY");
                    int ENDX = pFeature.Fields.FindField("ENDX");
                    int ENDY = pFeature.Fields.FindField("ENDY");
                    int DIMX = pFeature.Fields.FindField("DIMX");
                    int DIMY = pFeature.Fields.FindField("DIMY");
                    int TEXTX = pFeature.Fields.FindField("TEXTX");
                    int TEXTY = pFeature.Fields.FindField("TEXTY");
                    int DIMTYPE = pFeature.Fields.FindField("DIMTYPE");
                    int EXTANGLE = pFeature.Fields.FindField("EXTANGLE");
                    int STYLEID = pFeature.Fields.FindField("STYLEID");
                    int USECUSTOMLENGTH = pFeature.Fields.FindField("USECUSTOMLENGTH");
                    int CUSTOMLENGTH = pFeature.Fields.FindField("CUSTOMLENGTH");
                    int DIMDISPLAY = pFeature.Fields.FindField("DIMDISPLAY");
                    int EXTDISPLAY = pFeature.Fields.FindField("EXTDISPLAY");
                    int MARKERDISPLAY = pFeature.Fields.FindField("MARKERDISPLAY");
                    int TEXTANGLE = pFeature.Fields.FindField("TEXTANGLE");
                    int SHAPE = pFeature.Fields.FindField("SHAPE");
                    int SHAPESTArea = pFeature.Fields.FindField("SHAPE.STArea()");
                    int SHAPESTLength = pFeature.Fields.FindField("SHAPE.STLength()");


                    // pFeature.Value[dimLength] = 36.169741;
                    pFeature.Value[BEGINX] = pPoint1.X;
                    pFeature.Value[BEGINY] = pPoint1.Y;
                    pFeature.Value[ENDX] = pPoint2.X;
                    pFeature.Value[ENDY] = pPoint2.Y;
                    pFeature.Value[DIMX] = pPoint3.X;
                    pFeature.Value[DIMY] = pPoint3.Y;
                    //pFeature.Value[TEXTX] = null;
                    //pFeature.Value[TEXTY] = null;
                    pFeature.Value[DIMTYPE] = 0;
                    pFeature.Value[EXTANGLE] = 1.570796;
                    pFeature.Value[STYLEID] = 0;
                    pFeature.Value[USECUSTOMLENGTH] = 0;
                    pFeature.Value[CUSTOMLENGTH] = 0;
                    //pFeature.Value[DIMDISPLAY] = null;
                    //pFeature.Value[EXTDISPLAY] = null;
                    //pFeature.Value[MARKERDISPLAY] = null;
                    pFeature.Value[TEXTANGLE] = 0;


                    pFeature.Store();

                    workspaceEdit.StopEditOperation();
                    workspaceEdit.StopEditing(true);
                    mxDoc.ActiveView.Refresh();

                    pPoint1 = null;
                    pPoint2 = null;
                    pPoint3 = null;

                    ArcMap.Application.CurrentTool.Refresh();
                }
}

 

 

 

 

 

   

0 Kudos
0 Replies