Community
Civil 3D Customization
Welcome to Autodesk’s AutoCAD Civil 3D Forums. Share your knowledge, ask questions, and explore popular AutoCAD Civil 3D Customization topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Example C# code to temporarily show High/Low points of a featureline

18 REPLIES 18
SOLVED
Reply
Message 1 of 19
Jeff_M
1808 Views, 18 Replies

Example C# code to temporarily show High/Low points of a featureline

As I mentioned on the main C3D Forum, here is the source code and an attached Zip file with a compiled .NET dll.

 

// Example to display the High and Low points of a Featureline or 3DPolyline using temporary
// markers which are removed upon command completion.
// Jeff Mishler, permission to use this code for personal or corporate use is hereby granted.
// No commercial use is authorized.
// Copyright 2011

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.Land.DatabaseServices;
using aGi = Autodesk.AutoCAD.GraphicsInterface;

namespace FeatureHighLowExample
{
    public class FeatureHighLow
    {
        private static DBObjectCollection m_mrkers = new DBObjectCollection();
        private static IntegerCollection intColl = new IntegerCollection();

        [CommandMethod("FeatureHighLow")]
        public void featurehighlowcommand()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
            PromptEntityOptions entOpts = new PromptEntityOptions("\nSelect Featureline line or 3DPolyline: ");
            entOpts.SetRejectMessage("\n...not an allowed object type, try again.");
            entOpts.AddAllowedClass(typeof(FeatureLine), true);
            entOpts.AddAllowedClass(typeof(Polyline3d), true);
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptEntityResult res = ed.GetEntity(entOpts);
                while (res.Status == PromptStatus.OK)
                {
                    Curve crv = (Curve)tr.GetObject(res.ObjectId, OpenMode.ForRead);
                    Point3d low = new Point3d(0,0,999999);
                    Point3d high = new Point3d(0,0,-999999);
                    for (int i = (int)crv.StartParam; i <= (int)crv.EndParam; i++)
                    {
                        Point3d tmp = crv.GetPointAtParameter((double)i);
                        if (tmp.Z < low.Z)
                            low = tmp;
                        if (tmp.Z > high.Z)
                            high = tmp;
                    }
                    if (Math.Abs(low.Z - high.Z) < 0.001) //the object is level
                    {
                        ed.WriteMessage("\n Object is level!");
                    }
                    else
                    {
                        showHighpoint(high);
                        showLowpoint(low);
                    }
                    res = ed.GetEntity(entOpts);
                }
                removeMarkers();
                tr.Commit();
            }
        }

        private void showCircle(Point3d pt, int color)
        {
            //code to temporarily display a circle of the specified color. Uses a simple color index, 1-255.
            Vector3d v3d = new Vector3d(0, 0, 1); //this is the Normal of the object. May want to include code for use in UCS other than World
            double size = 10;//this is the radius of the circle. Adjust as desired, or, better yet, make this a ratio of the current screen size.
            Circle circle = new Circle(pt, v3d, size);
            m_mrkers.Add(circle);
            circle.ColorIndex = color;
            aGi.TransientManager.CurrentTransientManager.AddTransient(circle, aGi.TransientDrawingMode.DirectShortTerm, 128, intColl);
        }

        private void showHighpoint(Point3d pt)
        {
            showCircle(pt, 2); //add a yellow circle
        }


        private void showLowpoint(Point3d pt)
        {
            showCircle(pt, 3); //add a green circle
        }

        private void removeMarkers()
        {
            //remove them all when we are done....don't want screen clutter!
            for (int i = 0; i < m_mrkers.Count; i++)
            {
                aGi.TransientManager.CurrentTransientManager.EraseTransient(m_mrkers[i], intColl);
                m_mrkers[i].Dispose();
            }
            m_mrkers.Clear();
        }
    }
}

 

Jeff_M, also a frequent Swamper
EESignature
18 REPLIES 18
Message 2 of 19
neilyj666
in reply to: Jeff_M

Excellent code Jeff - very useful - but I wonder if it could be extended to put the high/lowpoints on a new layer and leave them on the drawing for reference?? I have no idea haow to program in C#.....!!!!!!

 

Thanks

neilyj (No connection with Autodesk other than using the products in the real world)
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


AEC Collection 2024 UKIE (mainly Civil 3D UKIE and IW)
Win 11 Pro x64, 1Tb Primary SSD, 1Tb Secondary SSD
64Gb RAM Intel(R) Xeon(R) W-11855M CPU @ 3.2GHz
NVIDIA RTX A5000 16Gb, Dual 27" Monitor, Dell Inspiron 7760
Message 3 of 19
Jeff_M
in reply to: neilyj666

Thanks, Neil. Sure the code could be amended as you describe, and it could also have these settings saved with different options, but that's something I don't have time to do right now. Perhaps someone else would like to take this on, otherwise I will do what I can when I get a chance.

 

In the mean time, attached is some slightly altered code, source and compiled dll, that leaves the markers after the command ends. They will remain on until either the new command CLEARHIGHLOWMARKERS is run, or the drawing is closed. 

Jeff_M, also a frequent Swamper
EESignature
Message 4 of 19
neilyj666
in reply to: Jeff_M

Thanks for the updated code - I'll not get a chance to play with it until after the holidays......... 

neilyj (No connection with Autodesk other than using the products in the real world)
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


AEC Collection 2024 UKIE (mainly Civil 3D UKIE and IW)
Win 11 Pro x64, 1Tb Primary SSD, 1Tb Secondary SSD
64Gb RAM Intel(R) Xeon(R) W-11855M CPU @ 3.2GHz
NVIDIA RTX A5000 16Gb, Dual 27" Monitor, Dell Inspiron 7760
Message 5 of 19
Jeff_M
in reply to: neilyj666

I was just given the rest of the day off (it's only 11 a.m. Friday here). So looks like I will have a bit of time to play some more with this.

 

 

Jeff_M, also a frequent Swamper
EESignature
Message 6 of 19
Jeff_M
in reply to: neilyj666

Attached is a zip file containing a new .NET DLL and the complete Visual Studio 2008 C# Project. This version has a Settings form, accessible any time at the Select object prompt is displayed. The settings are saved in a simple Text file in the ProgramData\C3DUtilities folder. I have limited the color options for the Temporary markers to colors 1-15, and the size of all markers may be 1-25 units. The command remains "FeatureHighLow" and the "ClearMarkers" command is still there to remove any Temp markers added using the Session option.

 

The VS Project was built against C3D2010 but runs in 2010-2012. If loading the project on a system without C3D2010 installed the references for the Acad, Aec, and AcDb dll's will need to be updated.

 

I doubt I will be making any updates to this code, although others may freely do so as long as it isn't included in any ForSale product. I hope at least 1 or 2 people find it useful to use and/or to see how one can implement custom commands using .NET & C# (although, since I'm far from a professional programmer, this may not be a shining example of good coding practice, but it works for me Smiley Wink )

 

Enjoy & Merry Christmas!

Jeff_M, also a frequent Swamper
EESignature
Message 7 of 19
neilyj666
in reply to: Jeff_M

Hats off to Jeff again - perfect tool..... can only give one kudos though

neilyj (No connection with Autodesk other than using the products in the real world)
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


AEC Collection 2024 UKIE (mainly Civil 3D UKIE and IW)
Win 11 Pro x64, 1Tb Primary SSD, 1Tb Secondary SSD
64Gb RAM Intel(R) Xeon(R) W-11855M CPU @ 3.2GHz
NVIDIA RTX A5000 16Gb, Dual 27" Monitor, Dell Inspiron 7760
Message 8 of 19
ookumo
in reply to: Jeff_M

So, how does this code line up with later versions of C3D. I have C3D 2019, and i've been having trouble NETLOADing into C3D.

Message 9 of 19
Jeff_M
in reply to: ookumo

It would need to be rebuilt with the newer Acad/C3D libraries.
Jeff_M, also a frequent Swamper
EESignature
Message 10 of 19
Jeff_M
in reply to: ookumo

Attached is a zip file with a newly compiled dll that should work in C3D 2018-2020 (and may work in future releases). Be sure to Unblock the ZIP file prior to extracting the file.

Jeff_M, also a frequent Swamper
EESignature
Message 11 of 19
ookumo
in reply to: Jeff_M

unfortunately, C3D couldn't load the assembly.

Message 12 of 19
Jeff_M
in reply to: ookumo

What error did it give, if any?
Jeff_M, also a frequent Swamper
EESignature
Message 13 of 19
ookumo
in reply to: Jeff_M

The error is below

 

"Assembly file name: C:\Users\Robo\Favorites\Useful Info\CAD EXE's\FeaturelineTools\FeaturelineTools.dll
Cannot load assembly. Error details: System.IO.FileLoadException: Could not load file or assembly 'file:///C:\Users\Robo\Favorites\Useful Info\CAD EXE's\FeaturelineTools\FeaturelineTools.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
File name: 'file:///C:\Users\Robo\Favorites\Useful Info\CAD EXE's\FeaturelineTools\FeaturelineTools.dll' ---> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at Autodesk.AutoCAD.Runtime.ExtensionLoader.Load(String fileName)
at loadmgd()"

Message 14 of 19
Jeff_M
in reply to: ookumo

That is typically the error thrown when you haven't Unblocked the ZIP file prior to extracting the DLL. Delete the DLL, find the ZIP and Unblock it, then re-extract the DLL. For example:

 

Unblock ZIP file.png

Jeff_M, also a frequent Swamper
EESignature
Message 15 of 19
neilyj666
in reply to: Jeff_M

@Jeff_M - this is working great for me.

I'd like to add this as a new command to my right click shortcut menu - I know how to add to the right click menu but not sure what to add in the macro box to create the new command - can you advise please?

 

2019-10-21 10_35_49-Window.jpg

neilyj (No connection with Autodesk other than using the products in the real world)
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


AEC Collection 2024 UKIE (mainly Civil 3D UKIE and IW)
Win 11 Pro x64, 1Tb Primary SSD, 1Tb Secondary SSD
64Gb RAM Intel(R) Xeon(R) W-11855M CPU @ 3.2GHz
NVIDIA RTX A5000 16Gb, Dual 27" Monitor, Dell Inspiron 7760
Message 16 of 19
ookumo
in reply to: Jeff_M

Thank you so much, Jeff. This was extremely useful. It works just fine. By the way, could you point me in the direction of resources for AutoCAD programming. It would be useful to have the know how to do what you do.

Message 17 of 19
Jeff_M
in reply to: neilyj666

@neilyj666 this should work:

^C^Cnetload "C:/PATHTOTHEFILE/FeaturelineTools.dll" FeatureHighLow 

Jeff_M, also a frequent Swamper
EESignature
Message 18 of 19
Jeff_M
in reply to: ookumo

@ookumo you're welcome.

 

First, I bought the book "C# for Dummies".

For general learning I used Google a lot and found sites that helped with learning c# such as https://stackoverflow.com and https://www.codeproject.com

For AutoCAD/Civil3D I used the Autodesk forums .NET & Civil 3D Customization as well as TheSwamp.org 

Jeff_M, also a frequent Swamper
EESignature
Message 19 of 19
neilyj666
in reply to: Jeff_M

@Jeff_M Many thanks - I'll try it in the few minutes...:)

 

EDIT: works with no problem...😁

neilyj (No connection with Autodesk other than using the products in the real world)
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


AEC Collection 2024 UKIE (mainly Civil 3D UKIE and IW)
Win 11 Pro x64, 1Tb Primary SSD, 1Tb Secondary SSD
64Gb RAM Intel(R) Xeon(R) W-11855M CPU @ 3.2GHz
NVIDIA RTX A5000 16Gb, Dual 27" Monitor, Dell Inspiron 7760

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


 

Autodesk Design & Make Report