.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

declare an array of a Point3d type in C# and resize it

8 REPLIES 8
Reply
Message 1 of 9
rashil
2489 Views, 8 Replies

declare an array of a Point3d type in C# and resize it

Can somebody help me here? I'm very new to C#.

using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Interop; namespace AutoCADProfileStationing { public class Commands { [CommandMethod("ACADbatch")] static public void Main(string[] args) { OpenFileDialog fdlg = new OpenFileDialog(); fdlg.Title = "C# Corner Open File Dialog"; fdlg.InitialDirectory = @"c:\"; fdlg.Filter = "AutoCAD .dwg files (*.dwg*)|*.dwg*"; fdlg.FilterIndex = 1; fdlg.RestoreDirectory = true; fdlg.Multiselect = true; if (fdlg.ShowDialog() == DialogResult.OK) { foreach (string fName in fdlg.FileNames) { Database db = new Database(false, true); using (db) { try { db.ReadDwgFile( fName, FileShare.ReadWrite, false, "" ); } catch (System.Exception) { return; } Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { DBDictionary layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite); foreach (DBDictionaryEntry entry in layoutDict) { if (entry.Key != "Model") { Layout layout = (Layout)tr.GetObject(entry.Value, OpenMode.ForWrite); BlockTableRecord layoutBtr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite, false); if (!layoutBtr.IsAnonymous) { int count = 0; Line[] callLines = null; Polyline2d profile = null; Polyline2d pipeProfile = null; MLeader myMLeader = null; MLeader ngMLeader = null; MLeader topMLeader = null; int textCount = 0; DBText[] callText = null; Point3dCollection pts = new Point3dCollection(); foreach (ObjectId id in layoutBtr) { Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite); Line myLine = ent as Line; if (myLine != null) { if (myLine.Layer == "Profile_Feature") { count = count + 1; callLines[count] = myLine; } } Polyline2d myPLine = ent as Polyline2d; if (myPLine != null) { if (myPLine.Layer == "Profile_Natural Gnd Line") { profile = myPLine; } else if (myPLine.Layer == "Profile_As-Built") { pipeProfile = myPLine; } } MLeader tempLeader = ent as MLeader; if (tempLeader != null) { if (tempLeader.Layer == "Text_Profile" && tempLeader.MText.Text == @"{\C1;5' COV.}" && tempLeader.GetVertex(0, 0).X < 250) { myMLeader = tempLeader; } else if (tempLeader.Layer == "Text_Profile" && tempLeader.MText.Text == @"{\C1;NATURAL GROUND}" && tempLeader.GetVertex(0, 0).X < 250) { ngMLeader = tempLeader; } else if (tempLeader.Layer == "Text_Profile" && tempLeader.MText.Text == @"{\C1;AS-BUILT T.O.P.\P20"" NGL PIPELINE}" && tempLeader.GetVertex(0, 0).X < 250) { topMLeader = tempLeader; } } DBText myText = ent as DBText; if (myText != null) { textCount = textCount + 1; callText[textCount] = myText; } } Point3d profileMid; Point3d pipeProfileMid; profileMid = profile.GetPointAtDist(profile.Length / 2); pipeProfileMid = pipeProfile.GetPointAtDist(pipeProfile.Length / 2); Point3d[] intPoint = new Point3d[callLines.Count()]; try { for (int i = 1; i < callLines.Length +1 ; i++) { intPoint[i] = callLines[i].IntersectWith(profile, Intersect.ExtendThis, pts, IntPtr.Zero, IntPtr.Zero); } } catch (System.Exception) { return; } } } } } } } } } } }

 

Tags (2)
8 REPLIES 8
Message 2 of 9
fieldguy
in reply to: rashil

There are several examples on this forum that use Point3DCollection.

Message 3 of 9
cdinten
in reply to: rashil

Using Point3dCollection is more convenient than array,it's resized dynamicly according to the number of elements.
Message 4 of 9
rashil
in reply to: fieldguy

In my code I also need to create a dynamic array or line and text elements. I think that the way I have done this is incorrect. What is the best way to create such an array?
Message 5 of 9
rashil
in reply to: rashil

Why am I getting an error on this line:

 

                                                intPoint.Add(callLines.ElementAt(i).IntersectWith(profile, Intersect.ExtendThis, pts, IntPtr.Zero, IntPtr.Zero));
                                                intPoint2.Add(callLines.ElementAt(i).IntersectWith(pipeProfile, Intersect.ExtendThis, pts, IntPtr.Zero, IntPtr.Zero));

 

Error 1 The best overloaded method match for 'System.Collections.Generic.List<Autodesk.AutoCAD.Geometry.Point3d>.Add(Autodesk.AutoCAD.Geometry.Point3d)' has some invalid arguments \\willbros.local\wei\users\rashil\My Documents\Visual Studio 2010\Projects\AutoCADProfileStationing\AutoCADProfileStationing\Class1.cs 141 49 AutoCADProfileStationing

 

Error 2 Argument 1: cannot convert from 'void' to 'Autodesk.AutoCAD.Geometry.Point3d' \\willbros.local\wei\users\rashil\My Documents\Visual Studio 2010\Projects\AutoCADProfileStationing\AutoCADProfileStationing\Class1.cs 141 63 AutoCADProfileStationing

 

 

Here is the entire code thus far:

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Interop;

namespace AutoCADProfileStationing
{
    public class Commands
    {
        [CommandMethod("ACADbatch")]
        static public void Main(string[] args)
        {

            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "C# Corner Open File Dialog";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "AutoCAD .dwg files (*.dwg*)|*.dwg*";
            fdlg.FilterIndex = 1;
            fdlg.RestoreDirectory = true;
            fdlg.Multiselect = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {

                foreach (string fName in fdlg.FileNames)

                {
                    Database db = new Database(false, true);
                    using (db)
                    {
                        try
                        {
                            db.ReadDwgFile(
                                fName,
                                FileShare.ReadWrite,
                                false,
                                ""
                                );
                        }
                        catch (System.Exception)
                        {
                            return;
                        }

                        Transaction tr =
                            db.TransactionManager.StartTransaction();
                        using (tr)
                        {
                            DBDictionary layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite);

                            foreach (DBDictionaryEntry entry in layoutDict)
                            {
                                if (entry.Key != "Model")
                                {
                                    Layout layout = (Layout)tr.GetObject(entry.Value, OpenMode.ForWrite);
                                    BlockTableRecord layoutBtr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite, false);
                                    if (!layoutBtr.IsAnonymous)
                                    {
                                        List<Line> callLines = new List<Line>();
                                        Polyline2d profile = null;
                                        Polyline2d pipeProfile = null;
                                        MLeader myMLeader = null;
                                        MLeader ngMLeader = null;
                                        MLeader topMLeader = null;
                                        List<DBText> callText = new List<DBText>();
                                        Point3dCollection pts = new Point3dCollection();
                                        
                                        
                                        foreach (ObjectId id in layoutBtr)
                                        {
                                            Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                                            Line myLine = ent as Line;
                                            if (myLine != null)
                                            {
                                                if (myLine.Layer == "Profile_Feature")
                                                {
                                                    callLines.Add(myLine);
                                                }
                                            }
                                            Polyline2d myPLine = ent as Polyline2d;
                                            if (myPLine != null)
                                            {
                                                if (myPLine.Layer == "Profile_Natural Gnd Line")
                                                {
                                                    profile = myPLine;
                                                }
                                                else if (myPLine.Layer == "Profile_As-Built")
                                                {
                                                    pipeProfile = myPLine;
                                                }
                                            }
                                            MLeader tempLeader = ent as MLeader;
                                            if (tempLeader != null)
                                            {
                                                if (tempLeader.Layer == "Text_Profile"
                                                    && tempLeader.MText.Text == @"{\C1;5' COV.}"
                                                    && tempLeader.GetVertex(0, 0).X < 250)
                                                {
                                                    myMLeader = tempLeader;
                                                }
                                                else if (tempLeader.Layer == "Text_Profile"
                                                    && tempLeader.MText.Text == @"{\C1;NATURAL GROUND}"
                                                    && tempLeader.GetVertex(0, 0).X < 250)
                                                {
                                                    ngMLeader = tempLeader;
                                                }
                                                else if (tempLeader.Layer == "Text_Profile"
                                                    && tempLeader.MText.Text == @"{\C1;AS-BUILT T.O.P.\P20"" NGL PIPELINE}"
                                                    && tempLeader.GetVertex(0, 0).X < 250)
                                                {
                                                    topMLeader = tempLeader;
                                                }
                                            }
                                            DBText myText = ent as DBText;
                                            if (myText != null)
                                            {
                                                callText.Add(myText);
                                            }
                                        }

                                        Point3d profileMid;
                                        Point3d pipeProfileMid;

                                        profileMid = profile.GetPointAtDist(profile.Length / 2);
                                        pipeProfileMid = pipeProfile.GetPointAtDist(pipeProfile.Length / 2);
                                        List<Point3d> intPoint = null;
                                        List<Point3d> intPoint2 = null;

                                        try
                                        {
                                            for (int i = 0; i < callLines.Count ; i++)
                                            {
                                                intPoint.Add(callLines.ElementAt(i).IntersectWith(profile, Intersect.ExtendThis, pts, IntPtr.Zero, IntPtr.Zero));
                                                intPoint2.Add(callLines.ElementAt(i).IntersectWith(pipeProfile, Intersect.ExtendThis, pts, IntPtr.Zero, IntPtr.Zero));
                                            }
                                        }

                                        catch (System.Exception)
                                        {
                                            return;
                                        }

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

 

Message 6 of 9
hgasty1001
in reply to: rashil

Hi,

 

Not sure what type of "array" you need as i don't know exactly what are you trying to do, I think that a little browsing on System.Collection.Generic namespace should help you to choose what kind of structure to use: Collections

 

Gaston Nunez

Message 7 of 9
Hallex
in reply to: rashil

Try instead:
((Point3d)callLines.ElementAt(i)).IntersectWith..etc
same with other points
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 9
rashil
in reply to: Hallex

This does not help, code returns same error.

Message 9 of 9
jeff
in reply to: rashil

List<Point3d> intPoint = null;

 

Your not going to have much luck using a variable that points to nothing.

List<Point3d> intPoint = new List<Point3D>();

 

new will create a List<Point3d> object and set inpoint to reference it.

 

Not sure where you can find a "dynamic" array assuming the items are sequential in memory, and would need to know how much memory to allocate.

 

A List<T> uses an array under the hood and once the array fills up it creates a new one twice as big and copies items over.

 

 

 

 

You can also find your answers @ TheSwamp

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost