Polyline ReadPolyline(List <netDxf.Entities.LwPolylineVertex> vertices, bool isClosed, double x, double y)
        {
            Polyline poly = new Polyline();

            for (int i = 0; i < vertices.Count; i++)
            {
                netDxf.Entities.LwPolylineVertex vertex = vertices[i];
                var point = new CADPoint(vertex.Position.X + x, vertex.Position.Y + y);
                poly.AddVertexAt(i, point);
            }
            if (isClosed)
            {
                poly.closed = true;
            }

            return(poly);
        }
Esempio n. 2
0
        /// <summary>
        /// build 2d dxf polyline.
        /// note: use RepeatFirstAtEnd extension to build a closed polyline
        /// </summary>
        public static netDxf.Entities.LwPolyline ToLwPolyline(this IEnumerable <Geometry> _geom, double tolLen, bool closed = true)
        {
            var geom = _geom.ToList();

            var N = Vector3D.ZAxis;

            var cs = new CoordinateSystem3D(Vector3D.Zero, N, CoordinateSystem3DAutoEnum.AAA);

            var pvtx = new List <netDxf.Entities.LwPolylineVertex>();

            Vector3D lastPt = null;

            for (int i = 0; i < geom.Count; ++i)
            {
                Vector3D from = null;
                Vector3D to   = null;

                switch (geom[i].Type)
                {
                case GeometryType.Vector3D:
                {
                    to = geom[i] as Vector3D;
                    var lwpv = new netDxf.Entities.LwPolylineVertex(to.ToVector2());
                    pvtx.Add(lwpv);
                    lastPt = to;
                }
                break;

                case GeometryType.Line3D:
                {
                    var seg = geom[i] as Line3D;
                    from = seg.From;
                    to   = seg.To;

                    if (lastPt == null || lastPt.EqualsTol(tolLen, from))
                    {
                        var lwpv = new netDxf.Entities.LwPolylineVertex(from.ToUCS(cs).ToVector2());
                        pvtx.Add(lwpv);
                        lastPt = to;
                    }
                    else
                    {
                        var lwpv = new netDxf.Entities.LwPolylineVertex(to.ToUCS(cs).ToVector2());
                        pvtx.Add(lwpv);
                        lastPt = from;
                    }
                }
                break;

                case GeometryType.Arc3D:
                {
                    var arc = geom[i] as Arc3D;
                    from = arc.From;
                    to   = arc.To;
                    var bulge = arc.Bulge(tolLen, arc.From, arc.To, N);

                    if (lastPt == null)
                    {
                        if (i < geom.Count - 1)
                        {
                            if (geom[i + 1].GeomFrom.EqualsTol(tolLen, to))
                            {
                                var lwpv = new netDxf.Entities.LwPolylineVertex(from.ToUCS(cs).ToVector2())
                                {
                                    Bulge = bulge
                                };
                                pvtx.Add(lwpv);
                                lastPt = to;
                            }
                            else
                            {
                                var lwpv = new netDxf.Entities.LwPolylineVertex(to.ToUCS(cs).ToVector2())
                                {
                                    Bulge = -bulge
                                };
                                pvtx.Add(lwpv);
                                lastPt = from;
                            }
                        }
                        else
                        {
                            var lwpv = new netDxf.Entities.LwPolylineVertex(from.ToUCS(cs).ToVector2())
                            {
                                Bulge = bulge
                            };
                            pvtx.Add(lwpv);
                            lastPt = to;
                        }
                    }
                    else
                    {
                        if (lastPt.EqualsTol(tolLen, from))
                        {
                            var lwpv = new netDxf.Entities.LwPolylineVertex(from.ToUCS(cs).ToVector2())
                            {
                                Bulge = bulge
                            };
                            pvtx.Add(lwpv);
                            lastPt = to;
                        }
                        else
                        {
                            var lwpv = new netDxf.Entities.LwPolylineVertex(to.ToUCS(cs).ToVector2())
                            {
                                Bulge = -bulge
                            };
                            pvtx.Add(lwpv);
                            lastPt = from;
                        }
                    }
                }
                break;
                }
            }

            if (!closed)
            {
                var lwpv = new netDxf.Entities.LwPolylineVertex(lastPt.ToUCS(cs).ToVector2());
                pvtx.Add(lwpv);
            }

            var lwpoly = new netDxf.Entities.LwPolyline(pvtx, isClosed: closed);

            lwpoly.Normal = N.Normalized();

            return(lwpoly);
        }
Esempio n. 3
0
        //extract mark vectors from DXF
        public static void Extract_Vectors(string filename, string layer_name, float mark_speed)
        {
            List<MarkVector> list_of_vectors = new List<MarkVector>();
            DxfDocument dxf = LoadDXF(filename);

            if (dxf==null)
            {
                MessageBox.Show("netDXF extraction from DXF failed, trying line by line.");
                dxf = LineByLineLoader(filename);

                //need a text only loader for this.  Python or not?
            }
            if (dxf==null)
            {
                MessageBox.Show("Line by line processing failed");
                list_of_vectors = null;
                //return list_of_vectors;
            }

            int number = 0;
            netDxf.Vector2 previous_point = new netDxf.Vector2(0.0d, 0.0d);
            netDxf.Entities.LwPolylineVertex avertex = new netDxf.Entities.LwPolylineVertex();
            netDxf.Entities.PolylineVertex bvertex = new netDxf.Entities.PolylineVertex();
            //convert LWPolylines to Mark Vectors
            foreach(netDxf.Entities.LwPolyline apolyline in dxf.LwPolylines)
            {
                if(apolyline.Layer.Name ==layer_name || layer_name =="All")
                {
                    for(int i=0; i<apolyline.Vertexes.Count; i++)
                    {
                        avertex = apolyline.Vertexes[i];
                        if(i==0)
                        {
                            previous_point.X=avertex.Location.X;
                            previous_point.Y=avertex.Location.Y;
                        }
                        if (i!=0)
                        {
                            MarkVector aMarkVector = new MarkVector((float)previous_point.X, (float)previous_point.Y, (float)avertex.Location.X, (float)avertex.Location.Y);
                            list_of_vectors.Add(aMarkVector);
                            number++;
                            previous_point.X=avertex.Location.X;
                            previous_point.Y=avertex.Location.Y;
                        }

                    }
                    if(apolyline.IsClosed==true)
                    {
                        MarkVector aMarkVector = new MarkVector((float)previous_point.X, (float)previous_point.Y, (float)apolyline.Vertexes[0].Location.X, (float)apolyline.Vertexes[0].Location.X);
                        list_of_vectors.Add(aMarkVector);
                    }
                }
            }

            //convert polylines to Mark Vectors
            foreach(netDxf.Entities.Polyline apolyline in dxf.Polylines)
            {
                for(int i=0; i<apolyline.Vertexes.Count; i++)
                {
                    bvertex = apolyline.Vertexes[i];
                    if(i==0)
                    {
                        previous_point.X=bvertex.Location.X;
                        previous_point.Y=bvertex.Location.Y;
                    }
                    if (i!=0)
                    {
                        MarkVector aMarkVector = new MarkVector((float)previous_point.X, (float)previous_point.Y, (float)bvertex.Location.X, (float)bvertex.Location.Y);
                        list_of_vectors.Add(aMarkVector);
                        number++;
                        previous_point.X=avertex.Location.X;
                        previous_point.Y=avertex.Location.Y;
                    }

                }
                if(apolyline.IsClosed==true)
                {
                    MarkVector aMarkVector = new MarkVector((float)previous_point.X, (float)previous_point.Y, (float)apolyline.Vertexes[0].Location.X, (float)apolyline.Vertexes[0].Location.X);
                    list_of_vectors.Add(aMarkVector);
                }
            }

            //convert Lines to Mark Vectors
            foreach(netDxf.Entities.Line aline in dxf.Lines)
            {
                if(aline.Layer.Name ==layer_name || layer_name =="All")
                {
                    number++;
                    MarkVector aMarkVector = new MarkVector((float)aline.StartPoint.X, (float)aline.StartPoint.Y, (float)aline.EndPoint.X, (float)aline.EndPoint.Y);
                    list_of_vectors.Add(aMarkVector);
                }
            }

            //convert arcs
            foreach(netDxf.Entities.Arc an_arc in dxf.Arcs)
            {
                if(an_arc.Layer.Name ==layer_name || layer_name =="All")
                {
                    number++;
                    //////TO DO/////////////
                }
            }

            //convert circles
            foreach(netDxf.Entities.Circle aCircle in dxf.Circles)
            {
                if(aCircle.Layer.Name ==layer_name || layer_name =="All")
                {
                    number++;
                    //////TO DO/////////////
                }
            }

            //MessageBox.Show(number.ToString());
            //
            //return list_of_vectors;
            vector_list = list_of_vectors;
        }
Esempio n. 4
0
        //Method to parse DXF line by line when LoadDXF fails
        public static DxfDocument LineByLineLoader(string filename)
        {
            vector_list.Clear();

            DxfDocument dxfdata = new DxfDocument();
            List<string> list_of_strings = new List<string>();
            //reset logic function
            reset_logic();

            entity_count =0;
            double intermediate =0.0d;  //used for nullable exchange
            int int_intermediate =0;

            bool in_block = false;
            bool in_entities =false;
            int LineCount  =0;
            string coder_string ="";
            string a_string ="";
            string layer_string ="";

            Vector2 start_point = new Vector2(0,0);
            Vector2 end_point = new Vector2(0,0);

            try
            {
                string line = null;
                bool found_file_end =false;
                System.IO.TextReader readFile = new StreamReader(filename);
                while (found_file_end==false)
                {
                    line = readFile.ReadLine();
                    if (line != null)
                    {
                        list_of_strings.Add(line);
                    }
                    else
                    {
                        found_file_end=true;
                    }
                }
                readFile.Close();
                readFile = null;
            }
               	    catch(Exception e)
            {
                error_string=e.ToString();
                return dxfdata;
            }

               	    for(int i=0;i<list_of_strings.Count; i+=2)   //read strings in pairs - first is code - second is value
               	    {
                LineCount = LineCount+1;
                coder_string = list_of_strings[i].Trim();
                try
                {
                    a_string = list_of_strings[i+1].Trim();
                }
                catch
                {
                    a_string="";
                }

                //check location in structure - only read from entities section
                if (coder_string =="0" && a_string=="BLOCK")
                {
                    in_block=true;
                }
                if(coder_string =="2" && a_string=="ENTITIES")
                {
                    in_entities=true;
                }
                if(coder_string =="0" && a_string=="ENDBLK")
                {
                    in_block=false;
                }
                if(coder_string =="0" && a_string=="ENDSEC")
                {
                    in_entities=false;
                }

                //read in layer info
                if(coder_string=="8" && in_block==false &&  in_entities==true && (ReadingLine==true || ReadingPolyline == true ||ReadingArc==true || ReadingCircle == true))
                {
                   layer_string = a_string;
                   FoundNewLayer=true;
                    //could populate a layer list here if needed

                }
                //read data
                if(coder_string=="10")
                {
                    double.TryParse(a_string, out intermediate);
                    X1=(double?)intermediate;
                }

                if (coder_string=="11")
                {
                    double.TryParse(a_string, out intermediate);
                    X2=(double?)intermediate;
                }

                if(coder_string=="20")
                {
                    double.TryParse(a_string, out intermediate);
                    Y1=(double?)intermediate;
                }

                if(coder_string=="21")
                {
                    double.TryParse(a_string, out intermediate);
                    Y2=(double?)intermediate;
                }

                if(coder_string=="40")
                {
                    double.TryParse(a_string, out intermediate);
                    radius=(double?)intermediate;
                }

                if(coder_string=="50")
                {
                    double.TryParse(a_string, out intermediate);
                    start_angle=(double?)intermediate;
                }

                if(coder_string=="51")
                {
                    double.TryParse(a_string, out intermediate);
                    end_angle=(double?)intermediate;
                }

                if(coder_string=="70")
                {
                    if(ReadingPolyline==true)
                    {
                        if(a_string=="1")
                        {
                            polyline_closed=true;
                        }
                        else
                        {
                            polyline_closed=false;
                        }
                    }
                }
                if(coder_string=="90")
                {
                    if(ReadingPolyline==true)
                    {
                        int.TryParse(a_string, out int_intermediate);
                        PointCount = (int?)int_intermediate;
                    }

                }

               	//flag start of a line
               	if(coder_string =="0" && a_string=="LINE" && in_block==false && in_entities==true)
               	{
               		if(ReadingLine==false)
               		{
                        reset_logic();
                        ReadingLine = true;
               		}
                    else
                    {
                        reset_logic();
                    }
               	}
                //add line if data complete
                if(ReadingLine==true && X1!=null && Y1 !=null && X2 !=null && Y2 != null)
                {
                    start_point = new Vector2((double)X1,(double)Y1);
                    end_point = new Vector2((double)X2,(double)Y2);

                    netDxf.Entities.Line aLine = new netDxf.Entities.Line(start_point,end_point);

                    aLine.Layer = new netDxf.Tables.Layer(layer_string);

                    dxfdata.AddEntity(aLine);
                    reset_logic();

                    entity_count++;
                }

                //flag start of a LWPOLYLINE
                if(coder_string =="0" && a_string=="LWPOLYLINE" && in_block==false && in_entities==true)
                {
                    if(ReadingPolyline == false)
                    {
                        reset_logic();
                        ReadingPolyline = true;
                    }
                    else
                    {
                        reset_logic();
                    }
                }

                //add point to polyline
                if(ReadingPolyline==true && X1 !=null && Y1!=null)
                {
                    netDxf.Entities.LwPolylineVertex aVertex = new netDxf.Entities.LwPolylineVertex(new Vector2((double)X1,(double)Y1),0.0d);

                    points.Add(aVertex);
                    X1 = null;
                    Y1 = null;
                }

                //add polyline if have enough points

                if(ReadingPolyline==true && points.Count==PointCount && PointCount !=null)
                {
                    netDxf.Entities.LwPolyline aPolyline = new netDxf.Entities.LwPolyline(points,polyline_closed);
                    aPolyline.Layer= new netDxf.Tables.Layer(layer_string);
                    dxfdata.AddEntity(aPolyline);
                    reset_logic();
                    entity_count++;

                }

                //flag star of a ARC
                if(coder_string =="0" && a_string=="ARC" && in_block == false)
                {
                    if(ReadingArc==false)
                    {
                        reset_logic();
                        ReadingArc = true;
                    }
                    else
                    {
                        reset_logic();
                    }
                }

                //add arc if data complete
                if(ReadingArc==true && X1 != null && Y1 !=null && radius !=null && start_angle != null && end_angle !=null)
                {
                    Vector2 center = new Vector2((double)X1,(double)Y1);
                    netDxf.Entities.Arc aArc = new netDxf.Entities.Arc(center,(double)radius,(double)start_angle,(double)end_angle);
                    aArc.Layer= new netDxf.Tables.Layer(layer_string);
                    dxfdata.AddEntity(aArc);
                    reset_logic();
                    entity_count++;
                }

                //flag star of a CIRCLE
                if (coder_string =="0" && a_string=="CIRCLE" && in_block ==false)
                {
                    if (ReadingArc==false)
                    {
                        reset_logic();
                        ReadingCircle =true;
                    }
                    else
                    {
                        reset_logic();
                    }
                }

                //add circle if data complete
                if(ReadingCircle==true && X1 !=null && Y1 !=null && radius != null)
                {
               			Vector2 center = new Vector2((double)X1,(double)Y1);
               			netDxf.Entities.Circle aCircle = new netDxf.Entities.Circle(center,(double)radius);
                    aCircle.Layer= new netDxf.Tables.Layer(layer_string);
                    dxfdata.AddEntity(aCircle);
                    reset_logic();
                    entity_count++;
                }

               	    }

            return dxfdata;
        }