How do I acquire a CogoPoint in C#

How do I acquire a CogoPoint in C#

gosbee30
Contributor Contributor
2,522 Views
10 Replies
Message 1 of 11

How do I acquire a CogoPoint in C#

gosbee30
Contributor
Contributor

How do I acquire a CogoPoint in C# I will need to get it's attributes such as the elevation, full description. I have some custom attributes I will need to write to. 

Is it possible to send parts of the raw description into different attribute fields. ie. for utility poles the raw description would be UP NM 10 NYS 15. I have a custom attribute Util Pole L1 = NM 10, Custom attribute Util Pole L2 = NYS 15. 

Thank you.

0 Likes
2,523 Views
10 Replies
Replies (10)
Message 2 of 11

Jeff_M
Consultant
Consultant

Do you wish to select the point on screen or get it by PointNumber? You can use UserDefinedProperties to add the additional information. If you could post an example dwg with some of the custom attributes assigned to points that may help as well.

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 3 of 11

Jeff_M
Consultant
Consultant

@gosbee30 here is an Editor extension method to get a CogoPoint by selection or by entering the Name or Number of the point:

        /// <summary>
        /// Allows the user to select a cogo point by picking an item in the drawing or
        /// entering the PointName or PointNumber.
        /// The cogo point is returned as an ObjectId.
        /// </summary>
        public static ObjectId SelectCogoPoint(this Editor ed, string prompt)
        {
            ObjectId result = ObjectId.Null;
            PromptEntityOptions entOpts = new PromptEntityOptions(prompt);
            entOpts.SetRejectMessage("...not a CogoPoint, try again.");
            entOpts.AddAllowedClass(typeof(CogoPoint), true);
            entOpts.Keywords.Add("Number");
            entOpts.Keywords.Add("nAme");
            PromptEntityResult entRes = ed.GetEntity(entOpts);
            if(entRes.Status== PromptStatus.Keyword)
            {
                if (entRes.StringResult == "Number")
                {
                    var intOpts = new PromptIntegerOptions("\n...Point Numner:");
                    intOpts.AllowNegative = false;
                    intOpts.AllowZero = false;
                    var intRes = ed.GetInteger(intOpts);
                    if (intRes.Status != PromptStatus.OK)
                        return result;
                    var points = CivilApplication.ActiveDocument.CogoPoints;
                    result = points.GetPointByPointNumber((uint)intRes.Value);
                }
                else if (entRes.StringResult == "nAme")
                {
                    var strOpts = new PromptStringOptions("\n...Point Name:");
                    var strRes = ed.GetString(strOpts);
                    if (strRes.Status != PromptStatus.OK || strRes.StringResult == "")
                        return result;
                    foreach (ObjectId id in CivilApplication.ActiveDocument.CogoPoints)
                    {
                        using (var pt = (CogoPoint)id.Open(OpenMode.ForRead))
                        {
                            if (pt.PointName == strRes.StringResult)
                            {
                                result = id;
                                pt.Close();
                                break;
                            }
                            pt.Close();
                        }
                    }
                }
            }
            else if (entRes.Status == PromptStatus.OK)
                result = entRes.ObjectId;
            return result;
        }

 

And here is an extension method for reading UDPs

        /// <summary>
        /// Method to get the string value of any PointUDP, useful for writing values out to text files
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="udp"></param>
        /// <returns></returns>
        public static string GetUDPValue(this CogoPoint pt, SettingsRoot sr, UDP udp)
        {
            string retVal = "";
            if (udp == null)
                return retVal;
            object val = null;
            try
            {
                val = pt.GetUDPValue((UDPDouble)udp);
                retVal = sr.FormatDistance((double)val);
            }
            catch { }
            try
            {
                val = pt.GetUDPValue((UDPBoolean)udp);
                retVal = val.ToString();
            }
            catch {  }
            try
            {
                val = pt.GetUDPValue((UDPInteger)udp);
                retVal = val.ToString();
            }
            catch {  }
            try
            {
                val = pt.GetUDPValue((UDPString)udp);
                retVal = val.ToString();
            }
            catch { }
            try
            {
                val = pt.GetUDPValue((UDPEnumeration)udp);
                retVal = val.ToString(); ;
            }
            catch {  }
            return retVal;
        }
Jeff_M, also a frequent Swamper
EESignature
Message 4 of 11

gosbee30
Contributor
Contributor
the code didn't show up as an options when I tried to add it to my button, 
Invert Class
public class InvertUtil
        {
public static ObjectId SelectCogoPoint(Editor edt, string prompt)
                {
Invert Form 
InvertUtil invt = new InvertUtil();
 private void BtnGetRim_Click(object sender, EventArgs e)
 {
       Editor edt = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
       string prompt = "Select a Manhole or Basin: ";
      invt.SelectCogoPoint(edt, prompt); // this was not an option in the invert field.
I am sending the Visual Basic 2019 project file and a copy of a template so you can take a look at it. The basic AutoCAD Programing I am comfortable with. Breaking into the Civil part has been problematic.
thank you.
0 Likes
Message 5 of 11

hippe013
Advisor
Advisor

I believe @Jeff_M wrote the examples as extension methods. The SelectCogoPoint method would be an extension method of the editor. 

String myPrompt = "Select a Manhole or Basin: ";
Editor edt = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ObjectId objId = edt.SelectCogoPoint(myPrompt);

 

 

Note: C# is not my first language. May contain syntax errors.

Message 6 of 11

Jeff_M
Consultant
Consultant

Yes, @hippe013 is correct. The extension methods also must reside in a static class. @gosbee30 you did not attach your project, did you intend to?

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 7 of 11

Jeff_M
Consultant
Consultant

For example:

using Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;

namespace Civil3D_Misc_Commands.Extensions
{
    public static class EditorExtensions
    {
      //previously posted extension methods here
    }
}

 

and in the form class add a using statement so it will be recognized:

using Civil3D_Misc_Commands.Extensions;

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 8 of 11

gosbee30
Contributor
Contributor
This is my latest C# project. 
0 Likes
Message 9 of 11

Jeff_M
Consultant
Consultant

@gosbee30 still no attachment.

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 10 of 11

gosbee30
Contributor
Contributor

I was able to find and get some vb.net code.

 Function ConvertToCogoPoint(per As PromptEntityResult)
        Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Dim dbObj As Autodesk.AutoCAD.DatabaseServices.DBObject = trans.GetObject(per.ObjectId, OpenMode.ForRead)
            Dim cogoPt As CogoPoint = TryCast(dbObj, CogoPoint)
            trans.Commit()
            Return cogoPt
        End Using
    End Function

I did this as a function so I can use it throughout my program. I am trying the attachment again. the other times I tried might have been because I was replying through my email rather than through the site.

0 Likes
Message 11 of 11

zrobert
Advocate
Advocate

Hi;

Just as a reminder of a fantastic blog for me, for anyone starting out or needing to be reminded of some things related to Civil 3d .net.

0 Likes