private XData ReadXDataRecord(string appId, ref CodeValuePair code) { XData xData = new XData(this.appIds[appId]); code = this.ReadCodePair(); while (code.Code >= 1000 && code.Code <= 1071) { if (code.Code == XDataCode.AppReg) break; XDataRecord xDataRecord = new XDataRecord(code.Code, code.Value); xData.XDataRecord.Add(xDataRecord); code = this.ReadCodePair(); } return xData; }
private ApplicationRegistry ReadApplicationId(ref CodeValuePair code) { string appId = string.Empty; string handle = string.Empty; code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { 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); } appId = code.Value; break; case 5: handle = code.Value; break; } code = this.ReadCodePair(); } return new ApplicationRegistry(appId) { Handle = handle }; }
private void ReadUnknowEntity(ref CodeValuePair code) { code = this.ReadCodePair(); while (code.Code != 0) { code = this.ReadCodePair(); } }
private Vertex ReadVertex(ref CodeValuePair code) { string handle = string.Empty; Layer layer = Layer.Default; AciColor color = AciColor.ByLayer; LineType lineType = LineType.ByLayer; Vector3d location = new Vector3d(); Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); float endThickness = 0.0f; float beginThickness = 0.0f; double bulge = 0.0; 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 = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: location.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 30: location.Z = double.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 = double.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 Vector2d(location.X, location.Y), // Bulge = bulge, // BeginThickness = beginThickness, // EndThickness = endThickness // }; // vertex.XData=xData; //} //return vertex; }
private Circle ReadCircle(ref CodeValuePair code) { var circle = new Circle(); Vector3d center = Vector3d.Zero; Vector3d normal = Vector3d.UnitZ; Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: circle.Handle = code.Value; code = this.ReadCodePair(); break; case 8: //layer code circle.Layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: //aci color code circle.Color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: //type line code circle.LineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 10: center.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: center.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 30: center.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 40: circle.Radius = double.Parse(code.Value); code = this.ReadCodePair(); break; case 39: circle.Thickness = float.Parse(code.Value); code = this.ReadCodePair(); break; case 210: normal.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 220: normal.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 230: normal.Z = double.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; } } circle.XData = xData; circle.Center = center; circle.Normal = normal; return circle; }
private Point ReadPoint(ref CodeValuePair code) { var point = new Point(); Vector3d location = Vector3d.Zero; Vector3d normal = Vector3d.UnitZ; Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: point.Handle = code.Value; code = this.ReadCodePair(); break; case 8: //layer code point.Layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: //aci color code point.Color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: //type line code point.LineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 10: location.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: location.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 30: location.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 39: point.Thickness = double.Parse(code.Value); code = this.ReadCodePair(); break; case 210: normal.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 220: normal.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 230: normal.Z = double.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; } } point.XData = xData; point.Location = location; point.Normal = normal; return point; }
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; double elevation = 0.0; float thickness = 0.0f; Vector3d normal = Vector3d.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 = double.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 = double.Parse(code.Value); code = this.ReadCodePair(); break; case 220: normal.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 230: normal.Z = double.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 Vector2d(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 Block ReadBlock(ref CodeValuePair code) { Layer layer = null; string name = string.Empty; string handle = string.Empty; Vector3d basePoint = Vector3d.Zero; List<IEntityObject> entities = new List<IEntityObject>(); Dictionary<string, AttributeDefinition> attdefs = new Dictionary<string, AttributeDefinition>(); code = this.ReadCodePair(); while (code.Value != StringCode.EndBlock) { switch (code.Code) { case 5: handle = code.Value; code = this.ReadCodePair(); break; case 8: layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 2: name = code.Value; code = this.ReadCodePair(); break; case 10: basePoint.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: basePoint.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 30: basePoint.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 3: name = code.Value; code = this.ReadCodePair(); break; case 0: // entity IEntityObject entity; entity = this.ReadBlockEntity(ref code); if (entity != null) if (entity.Type == EntityType.AttributeDefinition) attdefs.Add(((AttributeDefinition) entity).Id, (AttributeDefinition) entity); else entities.Add(entity); break; default: code = this.ReadCodePair(); break; } } // read the end bloc object until a new element is found code = this.ReadCodePair(); string endBlockHandle = string.Empty; Layer endBlockLayer = layer; while (code.Code != 0) { switch (code.Code) { case 5: endBlockHandle = code.Value; code = this.ReadCodePair(); break; case 8: endBlockLayer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; default: code = this.ReadCodePair(); break; } } Block block = new Block(name) { BasePoint = basePoint, Layer = layer, Entities = entities, Attributes = attdefs, Handle = handle, }; block.End.Handle = endBlockHandle; block.End.Layer = endBlockLayer; return block; }
private LightWeightPolyline ReadLightWeightPolyline(ref CodeValuePair code) { var pol = new LightWeightPolyline(); //int numVertexes; float constantWidth = 0.0F; LightWeightPolylineVertex v = new LightWeightPolylineVertex(); double vX = 0.0; Vector3d normal = Vector3d.UnitZ; Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: pol.Handle = code.Value; code = this.ReadCodePair(); break; case 8: pol.Layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: pol.Color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: pol.LineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 38: pol.Elevation = double.Parse(code.Value); code = this.ReadCodePair(); break; case 39: pol.Thickness = float.Parse(code.Value); code = this.ReadCodePair(); break; case 43: constantWidth = float.Parse(code.Value); code = this.ReadCodePair(); break; case 70: if (int.Parse(code.Value) == 0) { pol.IsClosed = false; } else if (int.Parse(code.Value) == 1) { pol.IsClosed = true; } code = this.ReadCodePair(); break; case 90: //numVertexes = int.Parse(code.Value); code = this.ReadCodePair(); break; case 10: v = new LightWeightPolylineVertex { BeginThickness = constantWidth, EndThickness = constantWidth }; vX = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: double vY = double.Parse(code.Value); v.Location = new Vector2d(vX, vY); pol.Vertexes.Add(v); code = this.ReadCodePair(); break; case 40: v.BeginThickness = float.Parse(code.Value); code = this.ReadCodePair(); break; case 41: v.EndThickness = float.Parse(code.Value); code = this.ReadCodePair(); break; case 42: v.Bulge = double.Parse(code.Value); code = this.ReadCodePair(); break; case 210: normal.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 220: normal.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 230: normal.Z = double.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; } } pol.Normal = normal; pol.XData = xData; return pol; }
private LineType ReadLineType(ref CodeValuePair code) { string handle = string.Empty; string name = string.Empty; string description = string.Empty; var segments = new List<float>(); 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 3: //descripción del tipo de línea description = code.Value; break; case 73: //number of segments (not needed) break; case 40: //length of the line type segments (not needed) break; case 49: segments.Add(float.Parse(code.Value)); break; } code = this.ReadCodePair(); } return new LineType(name) { Description = description, Segments = segments, Handle = handle }; }
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; Vector3d basePoint = Vector3d.Zero; Vector3d normal = Vector3d.UnitZ; Vector3d scale = new Vector3d(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 = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: basePoint.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 30: basePoint.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 41: scale.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 42: scale.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 43: scale.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 50: rotation = float.Parse(code.Value); code = this.ReadCodePair(); break; case 210: normal.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 220: normal.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 230: normal.Z = double.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; }
private Face3d ReadFace3D(ref CodeValuePair code) { var face = new Face3d(); Vector3d v0 = Vector3d.Zero; Vector3d v1 = Vector3d.Zero; Vector3d v2 = Vector3d.Zero; Vector3d v3 = Vector3d.Zero; Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: face.Handle = code.Value; code = this.ReadCodePair(); break; case 8: //layer code face.Layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: //aci color code face.Color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: //type line code face.LineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 10: v0.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: v0.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 30: v0.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 11: v1.X = float.Parse(code.Value); code = this.ReadCodePair(); break; case 21: v1.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 31: v1.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 12: v2.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 22: v2.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 32: v2.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 13: v3.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 23: v3.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 33: v3.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 70: face.EdgeFlags = (EdgeFlags) (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; } } face.FirstVertex = v0; face.SecondVertex = v1; face.ThirdVertex = v2; face.FourthVertex = v3; face.XData = xData; return face; }
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 Solid ReadSolid(ref CodeValuePair code) { var solid = new Solid(); Vector3d v0 = Vector3d.Zero; Vector3d v1 = Vector3d.Zero; Vector3d v2 = Vector3d.Zero; Vector3d v3 = Vector3d.Zero; Vector3d normal = Vector3d.UnitZ; Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: solid.Handle = code.Value; code = this.ReadCodePair(); break; case 8: //layer code solid.Layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: //aci color code solid.Color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: //type line code solid.LineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 10: v0.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: v0.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 30: v0.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 11: v1.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 21: v1.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 31: v1.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 12: v2.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 22: v2.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 32: v2.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 13: v3.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 23: v3.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 33: v3.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 70: solid.Thickness = float.Parse(code.Value); code = this.ReadCodePair(); break; case 210: normal.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 220: normal.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 230: normal.Z = double.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; } } solid.FirstVertex = v0; solid.SecondVertex = v1; solid.ThirdVertex = v2; solid.FourthVertex = v3; solid.Normal = normal; solid.XData = xData; return solid; }
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; Vector3d firstAlignmentPoint = Vector3d.Zero; Vector3d secondAlignmentPoint = Vector3d.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; Vector3d normal = Vector3d.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 = double.Parse(code.Value); break; case 20: firstAlignmentPoint.Y = double.Parse(code.Value); break; case 30: firstAlignmentPoint.Z = double.Parse(code.Value); break; case 11: secondAlignmentPoint.X = double.Parse(code.Value); break; case 21: secondAlignmentPoint.Y = double.Parse(code.Value); break; case 31: secondAlignmentPoint.Z = double.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 = double.Parse(code.Value); break; case 220: normal.Y = double.Parse(code.Value); break; case 230: normal.Z = double.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 Text ReadText(ref CodeValuePair code) { var text = new Text(); Vector3d firstAlignmentPoint = Vector3d.Zero; Vector3d secondAlignmentPoint = Vector3d.Zero; Vector3d normal = Vector3d.UnitZ; int horizontalAlignment = 0; int verticalAlignment = 0; Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: text.Handle = code.Value; code = this.ReadCodePair(); break; case 1: text.Value = code.Value; code = this.ReadCodePair(); break; case 8: //layer code text.Layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: //aci color code text.Color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: //type line code text.LineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 10: firstAlignmentPoint.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: firstAlignmentPoint.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 30: firstAlignmentPoint.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 11: secondAlignmentPoint.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 21: secondAlignmentPoint.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 31: secondAlignmentPoint.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 40: text.Height = float.Parse(code.Value); code = this.ReadCodePair(); break; case 41: text.WidthFactor = float.Parse(code.Value); code = this.ReadCodePair(); break; case 50: text.Rotation = float.Parse(code.Value); code = this.ReadCodePair(); break; case 51: text.ObliqueAngle = float.Parse(code.Value); code = this.ReadCodePair(); break; case 7: text.Style = this.GetTextStyle(code.Value); code = this.ReadCodePair(); break; case 72: horizontalAlignment = int.Parse(code.Value); code = this.ReadCodePair(); break; case 73: verticalAlignment = int.Parse(code.Value); code = this.ReadCodePair(); break; case 210: normal.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 220: normal.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 230: normal.Z = double.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; } } TextAlignment alignment = ObtainAlignment(horizontalAlignment, verticalAlignment); text.BasePoint = alignment == TextAlignment.BaselineLeft ? firstAlignmentPoint : secondAlignmentPoint; text.Normal = normal; text.Alignment = alignment; text.XData = xData; return text; }
private IEntityObject ReadBlockEntity(ref CodeValuePair code) { IEntityObject entity = null; switch (code.Value) { case DxfObjectCode.Arc: entity = this.ReadArc(ref code); break; case DxfObjectCode.Circle: entity = this.ReadCircle(ref code); break; case DxfObjectCode.Face3D: entity = this.ReadFace3D(ref code); break; case DxfObjectCode.Solid: entity = this.ReadSolid(ref code); break; case DxfObjectCode.Insert: code = this.ReadCodePair(); break; case DxfObjectCode.Line: entity = this.ReadLine(ref code); break; case DxfObjectCode.LightWeightPolyline: entity = this.ReadLightWeightPolyline(ref code); break; case DxfObjectCode.Polyline: entity = this.ReadPolyline(ref code); break; case DxfObjectCode.Text: entity = this.ReadText(ref code); break; case DxfObjectCode.AttributeDefinition: entity = this.ReadAttributeDefinition(ref code); break; default: ReadUnknowEntity(ref code); //code = this.ReadCodePair(); break; } return entity; }
private TextStyle ReadTextStyle(ref CodeValuePair code) { string handle = string.Empty; string name = string.Empty; string font = string.Empty; bool isVertical = false; bool isBackward = false; bool isUpsideDown = false; float height = 0.0f; float widthFactor = 0.0f; float obliqueAngle = 0.0f; code = this.ReadCodePair(); //leer los datos mientras no encontramos el código 0 que indicaría el final de la capa 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 3: 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); } font = code.Value; break; case 70: if (int.Parse(code.Value) == 4) { isVertical = true; } break; case 71: //orientación texto (normal) if (int.Parse(code.Value) == 6) { isBackward = true; isUpsideDown = true; } else if (int.Parse(code.Value) == 2) { isBackward = true; } else if (int.Parse(code.Value) == 4) { isUpsideDown = true; } break; case 40: height = float.Parse(code.Value); break; case 41: widthFactor = float.Parse(code.Value); break; case 42: //last text height used (not aplicable) break; case 50: obliqueAngle = (float.Parse(code.Value)); break; } code = this.ReadCodePair(); } return new TextStyle(name, font) { Height = height, IsBackward = isBackward, IsUpsideDown = isUpsideDown, IsVertical = isVertical, ObliqueAngle = obliqueAngle, WidthFactor = widthFactor, Handle = handle }; }
private Ellipse ReadEllipse(ref CodeValuePair code) { var ellipse = new Ellipse(); Vector3d center = Vector3d.Zero; Vector3d axisPoint = Vector3d.Zero; Vector3d normal = Vector3d.UnitZ; float ratio = 0; Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: ellipse.Handle = code.Value; code = this.ReadCodePair(); break; case 8: //layer code ellipse.Layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: //aci color code ellipse.Color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: //type line code ellipse.LineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 10: center.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: center.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 30: center.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 11: axisPoint.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 21: axisPoint.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 31: axisPoint.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 40: ratio = float.Parse(code.Value); code = this.ReadCodePair(); break; case 41: ellipse.StartAngle = double.Parse(code.Value)*MathHelper.RadToDeg; code = this.ReadCodePair(); break; case 42: ellipse.EndAngle = double.Parse(code.Value)*MathHelper.RadToDeg; code = this.ReadCodePair(); break; case 210: normal.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 220: normal.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 230: normal.Z = double.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; } } Vector3d ocsAxisPoint = MathHelper.Transform((Vector3d) axisPoint, (Vector3d) normal, MathHelper.CoordinateSystem.World, MathHelper.CoordinateSystem.Object); double rotation = Vector2d.AngleBetween(Vector2d.UnitX, new Vector2d(ocsAxisPoint.X, ocsAxisPoint.Y)); ellipse.MajorAxis = 2*axisPoint.Modulus(); ellipse.MinorAxis = ellipse.MajorAxis*ratio; ellipse.Rotation = rotation*MathHelper.RadToDeg; ellipse.Center = center; ellipse.Normal = normal; ellipse.XData = xData; return ellipse; }
private Line ReadLine(ref CodeValuePair code) { var line = new Line(); Vector3d start = Vector3d.Zero; Vector3d end = Vector3d.Zero; Vector3d normal = Vector3d.UnitZ; Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>(); code = this.ReadCodePair(); while (code.Code != 0) { switch (code.Code) { case 5: line.Handle = code.Value; code = this.ReadCodePair(); break; case 8: //layer code line.Layer = this.GetLayer(code.Value); code = this.ReadCodePair(); break; case 62: //aci color code line.Color = new AciColor(short.Parse(code.Value)); code = this.ReadCodePair(); break; case 6: //type line code line.LineType = this.GetLineType(code.Value); code = this.ReadCodePair(); break; case 10: start.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 20: start.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 30: start.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 11: end.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 21: end.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 31: end.Z = double.Parse(code.Value); code = this.ReadCodePair(); break; case 39: line.Thickness = double.Parse(code.Value); code = this.ReadCodePair(); break; case 210: normal.X = double.Parse(code.Value); code = this.ReadCodePair(); break; case 220: normal.Y = double.Parse(code.Value); code = this.ReadCodePair(); break; case 230: normal.Z = double.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; } } line.StartPoint = start; line.EndPoint = end; line.Normal = normal; line.XData = xData; return line; }