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; } } } } } } } } } } }
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; } } } } } } } } } } }
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
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.