private AttributeDefinition ReadAttributeDefinition(ref CodeValuePair code) { string handle = string.Empty; string id = string.Empty; string text = string.Empty; object value = null; AttributeFlags flags = AttributeFlags.Visible; Vector3f firstAlignmentPoint = Vector3f.Zero; Vector3f secondAlignmentPoint = Vector3f.Zero; Layer layer = Layer.Default; AciColor color = AciColor.ByLayer; LineType lineType = LineType.ByLayer; TextStyle style = TextStyle.Default; float height = 0; float widthFactor = 0; int horizontalAlignment = 0; int verticalAlignment = 0; float rotation = 0; Vector3f normal = Vector3f.UnitZ; code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: handle = code.Value; break; case 2: id = code.Value; break; case 3: text = code.Value; break; case 1: value = code.Value; break; case 8: //layer code layer = this.GetLayer(code.Value); break; case 62: //aci color code color = new AciColor(short.Parse(code.Value)); break; case 6: //type line code lineType = this.GetLineType(code.Value); break; case 70: flags = (AttributeFlags) int.Parse(code.Value); break; case 10: firstAlignmentPoint.X = float.Parse(code.Value); break; case 20: firstAlignmentPoint.Y = float.Parse(code.Value); break; case 30: firstAlignmentPoint.Z = float.Parse(code.Value); break; case 11: secondAlignmentPoint.X = float.Parse(code.Value); break; case 21: secondAlignmentPoint.Y = float.Parse(code.Value); break; case 31: secondAlignmentPoint.Z = float.Parse(code.Value); break; case 7: style = this.GetTextStyle(code.Value); break; case 40: height = float.Parse(code.Value); break; case 41: widthFactor = float.Parse(code.Value); break; case 50: rotation = float.Parse(code.Value); break; case 72: horizontalAlignment = int.Parse(code.Value); break; case 74: verticalAlignment = int.Parse(code.Value); break; case 210: normal.X = float.Parse(code.Value); break; case 220: normal.Y = float.Parse(code.Value); break; case 230: normal.Z = float.Parse(code.Value); break; } code = this.ReadCodePair(); } TextAlignment alignment = ObtainAlignment(horizontalAlignment, verticalAlignment); return new AttributeDefinition(id) { BasePoint = (alignment == TextAlignment.BaselineLeft ? firstAlignmentPoint : secondAlignmentPoint), Normal = normal, Alignment = alignment, Text = text, Value = value, Flags = flags, Layer = layer, Color = color, LineType = lineType, Style = style, Height = height, WidthFactor = widthFactor, Rotation = rotation, Handle = handle }; }
private Attribute ReadAttribute(Block block, ref CodeValuePair code) { string handle = string.Empty; AttributeDefinition attdef = null; Layer layer = Layer.Default; AciColor color = AciColor.ByLayer; LineType lineType = LineType.ByLayer; Object value = null; code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: handle = code.Value; break; case 2: attdef = block.Attributes[code.Value]; break; case 1: value = code.Value; break; case 8: //layer code layer = this.GetLayer(code.Value); break; case 62: //aci color code color = new AciColor(short.Parse(code.Value)); break; case 6: //type line code lineType = this.GetLineType(code.Value); break; } code = this.ReadCodePair(); } return new Attribute(attdef) { Color = color, Layer = layer, LineType = lineType, Value = value, Handle = handle }; }
private Vertex ReadVertex(ref CodeValuePair code) { string handle = string.Empty; Layer layer = Layer.Default; AciColor color = AciColor.ByLayer; LineType lineType = LineType.ByLayer; Vector3f location = new Vector3f(); Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); float endThickness = 0.0f; float beginThickness = 0.0f; float bulge = 0.0f; List<int> vertexIndexes = new List<int>(); VertexTypeFlags flags = VertexTypeFlags.PolylineVertex; code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: handle = code.Value; code = this.ReadCodePair(); break; case 8: layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: lineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 10: location.X = float.Parse(code.Value); code = this.ReadCodePair(); break; case 20: location.Y = float.Parse(code.Value); code = this.ReadCodePair(); break; case 30: location.Z = float.Parse(code.Value); code = this.ReadCodePair(); break; case 40: beginThickness = float.Parse(code.Value); code = this.ReadCodePair(); break; case 41: endThickness = float.Parse(code.Value); code = this.ReadCodePair(); break; case 42: bulge = float.Parse(code.Value); code = this.ReadCodePair(); break; case 70: flags = (VertexTypeFlags) int.Parse(code.Value); code = this.ReadCodePair(); break; case 71: vertexIndexes.Add(int.Parse(code.Value)); code = this.ReadCodePair(); break; case 72: vertexIndexes.Add(int.Parse(code.Value)); code = this.ReadCodePair(); break; case 73: vertexIndexes.Add(int.Parse(code.Value)); code = this.ReadCodePair(); break; case 74: vertexIndexes.Add(int.Parse(code.Value)); code = this.ReadCodePair(); break; case 1001: XData xDataItem = this.ReadXDataRecord(code.Value, ref code); xData.Add(xDataItem.ApplicationRegistry, xDataItem); break; default: if (code.Code >= 1000 && code.Code <= 1071) throw new DxfInvalidCodeValueEntityException(code.Code, code.Value, this.file, "The extended data of an entity must start with the application registry code " + this.fileLine); code = this.ReadCodePair(); break; } } return new Vertex { Flags = flags, Location = location, BeginThickness = beginThickness, Bulge = bulge, Color = color, EndThickness = endThickness, Layer = layer, LineType = lineType, VertexIndexes = vertexIndexes.ToArray(), XData = xData, Handle = handle }; //IVertex vertex; //if ((flags & (VertexTypeFlags.PolyfaceMeshVertex | VertexTypeFlags.Polygon3dMesh)) == (VertexTypeFlags.PolyfaceMeshVertex | VertexTypeFlags.Polygon3dMesh)) //{ // vertex = new PolyfaceMeshVertex // { // Location=location // }; // vertex.XData=xData; //} //else if ((flags & (VertexTypeFlags.PolyfaceMeshVertex)) == (VertexTypeFlags.PolyfaceMeshVertex)) //{ // vertex = new PolyfaceMeshFace(vertexIndexes.ToArray()); //} //else if ((flags & (VertexTypeFlags.Polyline3dVertex)) == (VertexTypeFlags.Polyline3dVertex)) //{ // vertex = new Polyline3dVertex // { // Location = location, // }; // vertex.XData=xData; //} //else //{ // vertex = new PolylineVertex // { // Location =new Vector2f(location.X, location.Y), // Bulge = bulge, // BeginThickness = beginThickness, // EndThickness = endThickness // }; // vertex.XData=xData; //} //return vertex; }
private IPolyline ReadPolyline(ref CodeValuePair code) { string handle = string.Empty; Layer layer = Layer.Default; AciColor color = AciColor.ByLayer; LineType lineType = LineType.ByLayer; PolylineTypeFlags flags = PolylineTypeFlags.OpenPolyline; float elevation = 0.0f; float thickness = 0.0f; Vector3f normal = Vector3f.UnitZ; List<Vertex> vertexes = new List<Vertex>(); Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); //int numVertexes = -1; //int numFaces = -1; code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: handle = code.Value; code = this.ReadCodePair(); break; case 8: layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: lineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 30: elevation = float.Parse(code.Value); code = this.ReadCodePair(); break; case 39: thickness = float.Parse(code.Value); code = this.ReadCodePair(); break; case 70: flags = (PolylineTypeFlags) (int.Parse(code.Value)); code = this.ReadCodePair(); break; case 71: //this field might not exist for polyface meshes, we cannot depend on it //numVertexes = int.Parse(code.Value); code = this.ReadCodePair(); code = this.ReadCodePair(); break; case 72: //this field might not exist for polyface meshes, we cannot depend on it //numFaces = int.Parse(code.Value); code = this.ReadCodePair(); break; case 210: normal.X = float.Parse(code.Value); code = this.ReadCodePair(); break; case 220: normal.Y = float.Parse(code.Value); code = this.ReadCodePair(); break; case 230: normal.Z = float.Parse(code.Value); code = this.ReadCodePair(); break; case 1001: XData xDataItem = this.ReadXDataRecord(code.Value, ref code); xData.Add(xDataItem.ApplicationRegistry, xDataItem); break; default: if (code.Code >= 1000 && code.Code <= 1071) throw new DxfInvalidCodeValueEntityException(code.Code, code.Value, this.file, "The extended data of an entity must start with the application registry code " + this.fileLine); code = this.ReadCodePair(); break; } } //begin to read the vertex list if (code.Value != DxfObjectCode.Vertex) throw new DxfEntityException(DxfObjectCode.Polyline, this.file, "Vertex not found in line " + this.fileLine); while (code.Value != StringCode.EndSequence) { if (code.Value == DxfObjectCode.Vertex) { Debug.Assert(code.Code == 0); Vertex vertex = this.ReadVertex(ref code); vertexes.Add(vertex); } } // read the end end sequence object until a new element is found if (code.Value != StringCode.EndSequence) throw new DxfEntityException(DxfObjectCode.Polyline, this.file, "End sequence entity not found in line " + this.fileLine); code = this.ReadCodePair(); string endSequenceHandle = string.Empty; Layer endSequenceLayer = layer; while (code.Code != 0) { switch (code.Code) { case 5: endSequenceHandle = code.Value; code = this.ReadCodePair(); break; case 8: endSequenceLayer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; default: code = this.ReadCodePair(); break; } } IPolyline pol; bool isClosed = false; if ((flags & PolylineTypeFlags.ClosedPolylineOrClosedPolygonMeshInM) == PolylineTypeFlags.ClosedPolylineOrClosedPolygonMeshInM) { isClosed = true; } //to avoid possible error between the vertex type and the polyline type //the polyline type will decide which information to use from the read vertex if ((flags & PolylineTypeFlags.Polyline3D) == PolylineTypeFlags.Polyline3D) { List<Polyline3dVertex> polyline3dVertexes = new List<Polyline3dVertex>(); foreach (Vertex v in vertexes) { Polyline3dVertex vertex = new Polyline3dVertex { Color = v.Color, Layer = v.Layer, LineType = v.LineType, Location = v.Location, Handle = v.Handle }; vertex.XData = v.XData; polyline3dVertexes.Add(vertex); } ////posible error avoidance, the polyline is marked as polyline3d code:(70,8) but the vertex is marked as PolylineVertex code:(70,0) //if (v.Type == EntityType.PolylineVertex) //{ // Polyline3dVertex polyline3dVertex = new Polyline3dVertex(((PolylineVertex)v).Location.X, ((PolylineVertex)v).Location.Y,0); // polyline3dVertexes.Add(polyline3dVertex); //} //else //{ // polyline3dVertexes.Add((Polyline3dVertex)v); //} //} pol = new Polyline3d(polyline3dVertexes, isClosed) { Handle = handle }; ((Polyline3d) pol).EndSequence.Handle = endSequenceHandle; ((Polyline3d) pol).EndSequence.Layer = endSequenceLayer; } else if ((flags & PolylineTypeFlags.PolyfaceMesh) == PolylineTypeFlags.PolyfaceMesh) { //the vertex list created contains vertex and face information List<PolyfaceMeshVertex> polyfaceVertexes = new List<PolyfaceMeshVertex>(); List<PolyfaceMeshFace> polyfaceFaces = new List<PolyfaceMeshFace>(); foreach (Vertex v in vertexes) { if ((v.Flags & (VertexTypeFlags.PolyfaceMeshVertex | VertexTypeFlags.Polygon3dMesh)) == (VertexTypeFlags.PolyfaceMeshVertex | VertexTypeFlags.Polygon3dMesh)) { PolyfaceMeshVertex vertex = new PolyfaceMeshVertex { Color = v.Color, Layer = v.Layer, LineType = v.LineType, Location = v.Location, Handle = v.Handle }; vertex.XData = xData; polyfaceVertexes.Add(vertex); } else if ((v.Flags & (VertexTypeFlags.PolyfaceMeshVertex)) == (VertexTypeFlags.PolyfaceMeshVertex)) { PolyfaceMeshFace vertex = new PolyfaceMeshFace { Color = v.Color, Layer = v.Layer, LineType = v.LineType, VertexIndexes = v.VertexIndexes, Handle = v.Handle }; vertex.XData = xData; polyfaceFaces.Add(vertex); } //if (v.Type == EntityType.PolyfaceMeshVertex) //{ // polyfaceVertexes.Add((PolyfaceMeshVertex) v); //} //else if (v.Type == EntityType.PolyfaceMeshFace) //{ // polyfaceFaces.Add((PolyfaceMeshFace) v); //} //else //{ // throw new EntityDxfException(v.Type.ToString(), this.file, "Error in vertex type."); //} } pol = new PolyfaceMesh(polyfaceVertexes, polyfaceFaces) { Handle = handle }; ((PolyfaceMesh) pol).EndSequence.Handle = endSequenceHandle; ((PolyfaceMesh) pol).EndSequence.Layer = endSequenceLayer; } else { List<PolylineVertex> polylineVertexes = new List<PolylineVertex>(); foreach (Vertex v in vertexes) { PolylineVertex vertex = new PolylineVertex { Location = new Vector2f(v.Location.X, v.Location.Y), BeginThickness = v.BeginThickness, Bulge = v.Bulge, Color = v.Color, EndThickness = v.EndThickness, Layer = v.Layer, LineType = v.LineType, Handle = v.Handle }; vertex.XData = xData; ////posible error avoidance, the polyline is marked as polyline code:(70,0) but the vertex is marked as Polyline3dVertex code:(70,32) //if (v.Type==EntityType.Polyline3dVertex) //{ // PolylineVertex polylineVertex = new PolylineVertex(((Polyline3dVertex)v).Location.X, ((Polyline3dVertex)v).Location.Y); // polylineVertexes.Add(polylineVertex); //} //else //{ // polylineVertexes.Add((PolylineVertex) v); //} polylineVertexes.Add(vertex); } pol = new Polyline(polylineVertexes, isClosed) { Thickness = thickness, Elevation = elevation, Normal = normal, Handle = handle }; ((Polyline) pol).EndSequence.Handle = endSequenceHandle; ((Polyline) pol).EndSequence.Layer = endSequenceLayer; } pol.Color = color; pol.Layer = layer; pol.LineType = lineType; pol.XData = xData; return pol; }
private Layer ReadLayer(ref CodeValuePair code) { string handle = string.Empty; string name = string.Empty; bool isVisible = true; AciColor color = null; LineType lineType = null; code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: handle = code.Value; break; case 2: if (string.IsNullOrEmpty(code.Value)) { throw new DxfInvalidCodeValueEntityException(code.Code, code.Value, this.file, "Invalid value " + code.Value + " in code " + code.Code + " line " + this.fileLine); } name = code.Value; break; case 62: short index; if (short.TryParse(code.Value, out index)) { if (index < 0) { isVisible = false; index = Math.Abs(index); } if (index > 256) { throw new DxfInvalidCodeValueEntityException(code.Code, code.Value, this.file, "Invalid value " + code.Value + " in code " + code.Code + " line " + this.fileLine); } } else { throw new DxfInvalidCodeValueEntityException(code.Code, code.Value, this.file, "Invalid value " + code.Value + " in code " + code.Code + " line " + this.fileLine); } color = new AciColor(short.Parse(code.Value)); break; case 6: if (string.IsNullOrEmpty(code.Value)) { throw new DxfInvalidCodeValueEntityException(code.Code, code.Value, this.file, "Invalid value " + code.Value + " in code " + code.Code + " line " + this.fileLine); } lineType = this.GetLineType(code.Value); break; } code = this.ReadCodePair(); } return new Layer(name) { Color = color, LineType = lineType, IsVisible = isVisible, Handle = handle }; }
private Insert ReadInsert(ref CodeValuePair code) { string handle = string.Empty; Vector3f basePoint = Vector3f.Zero; Vector3f normal = Vector3f.UnitZ; Vector3f scale = new Vector3f(1, 1, 1); float rotation = 0.0f; Block block = null; Layer layer = Layer.Default; AciColor color = AciColor.ByLayer; LineType lineType = LineType.ByLayer; List<Attribute> attributes = new List<Attribute>(); Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: handle = code.Value; code = this.ReadCodePair(); break; case 2: block = this.GetBlock(code.Value); if (block == null) throw new DxfEntityException(DxfObjectCode.Insert, this.file, "Block " + code.Value + " not defined line " + this.fileLine); code = this.ReadCodePair(); break; case 8: //layer code layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: //aci color code color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: //type line code lineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 10: basePoint.X = float.Parse(code.Value); code = this.ReadCodePair(); break; case 20: basePoint.Y = float.Parse(code.Value); code = this.ReadCodePair(); break; case 30: basePoint.Z = float.Parse(code.Value); code = this.ReadCodePair(); break; case 41: scale.X = float.Parse(code.Value); code = this.ReadCodePair(); break; case 42: scale.X = float.Parse(code.Value); code = this.ReadCodePair(); break; case 43: scale.X = float.Parse(code.Value); code = this.ReadCodePair(); break; case 50: rotation = float.Parse(code.Value); code = this.ReadCodePair(); break; case 210: normal.X = float.Parse(code.Value); code = this.ReadCodePair(); break; case 220: normal.Y = float.Parse(code.Value); code = this.ReadCodePair(); break; case 230: normal.Z = float.Parse(code.Value); code = this.ReadCodePair(); break; case 1001: XData xDataItem = this.ReadXDataRecord(code.Value, ref code); xData.Add(xDataItem.ApplicationRegistry, xDataItem); break; default: if (code.Code >= 1000 && code.Code <= 1071) throw new DxfInvalidCodeValueEntityException(code.Code, code.Value, this.file, "The extended data of an entity must start with the application registry code " + this.fileLine); code = this.ReadCodePair(); break; } } // if there are attributes string endSequenceHandle = string.Empty; Layer endSequenceLayer = Layer.Default; if (code.Value == DxfObjectCode.Attribute) { while (code.Value != StringCode.EndSequence) { if (code.Value == DxfObjectCode.Attribute) { Debug.Assert(code.Code == 0); Attribute attribute = this.ReadAttribute(block, ref code); attributes.Add(attribute); } } // read the end end sequence object until a new element is found code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: endSequenceHandle = code.Value; code = this.ReadCodePair(); break; case 8: endSequenceLayer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; default: code = this.ReadCodePair(); break; } } } Insert insert = new Insert(block) { Color = color, Layer = layer, LineType = lineType, InsertionPoint = basePoint, Rotation = rotation, Scale = scale, Normal = normal, Handle = handle }; insert.EndSequence.Handle = endSequenceHandle; insert.EndSequence.Layer = endSequenceLayer; insert.Attributes.Clear(); insert.Attributes.AddRange(attributes); insert.XData = xData; return insert; }