/// <summary> /// Creates a new HatchPatternLineDefinition that is a copy of the current instance. /// </summary> /// <returns>A new HatchPatternLineDefinition that is a copy of this instance.</returns> public object Clone() { HatchPatternLineDefinition copy = new HatchPatternLineDefinition { Angle = this.angle, Origin = this.origin, Delta = this.delta, }; foreach (double dash in this.dashPattern) { copy.DashPattern.Add(dash); } return(copy); }
/// <summary> /// Creates a new hatch pattern from the definition in a pat file. /// </summary> /// <param name="file">Pat file where the definition is located.</param> /// <param name="patternName">Name of the pattern definition that wants to be read (ignore case).</param> /// <returns>A Hatch pattern defined by the pat file.</returns> public static HatchPattern FromFile(string file, string patternName) { HatchPattern pattern = null; using (StreamReader reader = new StreamReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), true)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); if (line == null) { throw new FileLoadException("Unknown error reading pat file.", file); } // lines starting with semicolons are comments if (line.StartsWith(";")) { continue; } // every pattern definition starts with '*' if (!line.StartsWith("*")) { continue; } // reading pattern name and description int endName = line.IndexOf(','); // the first semicolon divides the name from the description that might contain more semicolons string name = line.Substring(1, endName - 1); string description = line.Substring(endName + 1, line.Length - endName - 1); // remove start and end spaces description = description.Trim(); if (!name.Equals(patternName, StringComparison.OrdinalIgnoreCase)) { continue; } // we have found the pattern name, the next lines of the file contains the pattern definition line = reader.ReadLine(); if (line == null) { throw new FileLoadException("Unknown error reading pat file.", file); } pattern = new HatchPattern(name, description); while (!reader.EndOfStream && !line.StartsWith("*") && !string.IsNullOrEmpty(line)) { List <double> dashPattern = new List <double>(); string[] tokens = line.Split(','); double angle = double.Parse(tokens[0]); Vector2 origin = new Vector2(double.Parse(tokens[1]), double.Parse(tokens[2])); Vector2 delta = new Vector2(double.Parse(tokens[3]), double.Parse(tokens[4])); // the rest of the info is optional if it exists define the dash pattern definition for (int i = 5; i < tokens.Length; i++) { dashPattern.Add(double.Parse(tokens[i])); } HatchPatternLineDefinition lineDefinition = new HatchPatternLineDefinition { Angle = angle, Origin = origin, Delta = delta, DashPattern = dashPattern }; pattern.lineDefinitions.Add(lineDefinition); pattern.type = HatchType.UserDefined; line = reader.ReadLine(); if (line == null) { throw new FileLoadException("Unknown error reading pat file.", file); } line = line.Trim(); } // there is no need to continue parsing the file, the info has been read break; } } return(pattern); }
/// <summary> /// Creates a new HatchPatternLineDefinition that is a copy of the current instance. /// </summary> /// <returns>A new HatchPatternLineDefinition that is a copy of this instance.</returns> public object Clone() { HatchPatternLineDefinition copy = new HatchPatternLineDefinition { Angle = this.angle, Origin = this.origin, Delta = this.delta, }; foreach (double dash in this.dashPattern) copy.DashPattern.Add(dash); return copy; }
/// <summary> /// Creates a new hatch pattern from the definition in a pat file. /// </summary> /// <param name="file">Pat file where the definition is located.</param> /// <param name="patternName">Name of the pattern definition that wants to be read (ignore case).</param> /// <returns>A Hatch pattern defined by the pat file.</returns> public static HatchPattern FromFile(string file, string patternName) { HatchPattern pattern = null; using (StreamReader reader = new StreamReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), true)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); if (line == null) throw new FileLoadException("Unknown error reading pat file.", file); // lines starting with semicolons are comments if (line.StartsWith(";")) continue; // every pattern definition starts with '*' if (!line.StartsWith("*")) continue; // reading pattern name and description int endName = line.IndexOf(','); // the first semicolon divides the name from the description that might contain more semicolons string name = line.Substring(1, endName - 1); string description = line.Substring(endName + 1, line.Length - endName - 1); // remove start and end spaces description = description.Trim(); if (!name.Equals(patternName, StringComparison.OrdinalIgnoreCase)) continue; // we have found the pattern name, the next lines of the file contains the pattern definition line = reader.ReadLine(); if (line == null) throw new FileLoadException("Unknown error reading pat file.", file); pattern = new HatchPattern(name, description); while (!reader.EndOfStream && !line.StartsWith("*") && !string.IsNullOrEmpty(line)) { string[] tokens = line.Split(','); double angle = double.Parse(tokens[0]); Vector2 origin = new Vector2(double.Parse(tokens[1]), double.Parse(tokens[2])); Vector2 delta = new Vector2(double.Parse(tokens[3]), double.Parse(tokens[4])); HatchPatternLineDefinition lineDefinition = new HatchPatternLineDefinition { Angle = angle, Origin = origin, Delta = delta, }; // the rest of the info is optional if it exists define the dash pattern definition for (int i = 5; i < tokens.Length; i++) lineDefinition.DashPattern.Add(double.Parse(tokens[i])); pattern.LineDefinitions.Add(lineDefinition); pattern.Type = HatchType.UserDefined; line = reader.ReadLine(); if (line == null) throw new FileLoadException("Unknown error reading pat file.", file); line = line.Trim(); } // there is no need to continue parsing the file, the info has been read break; } } return pattern; }
/// <summary> /// Creates a new hatch pattern from the definition in a PAT file. /// </summary> /// <param name="file">PAT file where the definition is located.</param> /// <param name="patternName">Name of the pattern definition that wants to be read (ignore case).</param> /// <returns>A Hatch pattern as defined in the PAT file.</returns> public static HatchPattern Load(string file, string patternName) { HatchPattern pattern = null; using (StreamReader reader = new StreamReader(File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), true)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); if (line == null) throw new FileLoadException("Unknown error reading pat file.", file); line = line.Trim(); // every pattern definition starts with '*' if (!line.StartsWith("*")) continue; // reading pattern name and description int endName = line.IndexOf(','); // the first semicolon divides the name from the description that might contain more semicolons string name = line.Substring(1, endName - 1); string description = line.Substring(endName + 1, line.Length - endName - 1); // remove start and end spaces description = description.Trim(); if (!name.Equals(patternName, StringComparison.OrdinalIgnoreCase)) continue; // we have found the pattern name, the next lines of the file contains the pattern definition line = reader.ReadLine(); if (line == null) throw new FileLoadException("Unknown error reading PAT file.", file); line = line.Trim(); pattern = new HatchPattern(name, description); while (!string.IsNullOrEmpty(line) && !line.StartsWith("*") && !line.StartsWith(";")) { string[] tokens = line.Split(','); if(tokens.Length < 5) throw new FileLoadException("The hatch pattern definition lines must contain at least 5 values.", file); double angle = double.Parse(tokens[0], NumberStyles.Float, CultureInfo.InvariantCulture); Vector2 origin = new Vector2( double.Parse(tokens[1], NumberStyles.Float, CultureInfo.InvariantCulture), double.Parse(tokens[2], NumberStyles.Float, CultureInfo.InvariantCulture)); Vector2 delta = new Vector2( double.Parse(tokens[3], NumberStyles.Float, CultureInfo.InvariantCulture), double.Parse(tokens[4], NumberStyles.Float, CultureInfo.InvariantCulture)); HatchPatternLineDefinition lineDefinition = new HatchPatternLineDefinition { Angle = angle, Origin = origin, Delta = delta, }; // the rest of the info is optional if it exists define the dash pattern definition for (int i = 5; i < tokens.Length; i++) lineDefinition.DashPattern.Add(double.Parse(tokens[i], NumberStyles.Float, CultureInfo.InvariantCulture)); pattern.LineDefinitions.Add(lineDefinition); pattern.Type = HatchType.UserDefined; if(reader.EndOfStream) break; line = reader.ReadLine(); if (line == null) throw new FileLoadException("Unknown error reading PAT file.", file); line = line.Trim(); } // there is no need to continue parsing the file, the info has been read break; } } return pattern; }
private List<HatchPatternLineDefinition> ReadHatchPatternDefinitionLine(double patternScale, double patternAngle, short numLines) { List<HatchPatternLineDefinition> lineDefinitions = new List<HatchPatternLineDefinition>(); this.chunk.Next(); for (int i = 0; i < numLines; i++) { Vector2 origin = Vector2.Zero; Vector2 delta = Vector2.Zero; double angle = this.chunk.ReadDouble(); // code 53 this.chunk.Next(); origin.X = this.chunk.ReadDouble(); // code 43 this.chunk.Next(); origin.Y = this.chunk.ReadDouble(); // code 44 this.chunk.Next(); delta.X = this.chunk.ReadDouble(); // code 45 this.chunk.Next(); delta.Y = this.chunk.ReadDouble(); // code 46 this.chunk.Next(); short numSegments = this.chunk.ReadShort(); // code 79 this.chunk.Next(); // Pattern fill data. In theory this should hold the same information as the pat file but for unknown reason the dxf requires global data instead of local. // this means we have to convert the global data into local, since we are storing the pattern line definition as it appears in the acad.pat file. double sinOrigin = Math.Sin(patternAngle*MathHelper.DegToRad); double cosOrigin = Math.Cos(patternAngle*MathHelper.DegToRad); origin = new Vector2(cosOrigin*origin.X/patternScale + sinOrigin*origin.Y/patternScale, -sinOrigin*origin.X/patternScale + cosOrigin*origin.Y/patternScale); double sinDelta = Math.Sin(angle*MathHelper.DegToRad); double cosDelta = Math.Cos(angle*MathHelper.DegToRad); delta = new Vector2(cosDelta*delta.X/patternScale + sinDelta*delta.Y/patternScale, -sinDelta*delta.X/patternScale + cosDelta*delta.Y/patternScale); HatchPatternLineDefinition lineDefiniton = new HatchPatternLineDefinition { Angle = angle - patternAngle, Origin = origin, Delta = delta, }; for (int j = 0; j < numSegments; j++) { // positive values means solid segments and negative values means spaces (one entry per element) lineDefiniton.DashPattern.Add(this.chunk.ReadDouble()/patternScale); // code 49 this.chunk.Next(); } lineDefinitions.Add(lineDefiniton); } return lineDefinitions; }
private static void CustomHatchPattern() { DxfDocument dxf = new DxfDocument(); LwPolyline poly = new LwPolyline(); poly.Vertexes.Add(new LwPolylineVertex(-10, -10)); poly.Vertexes.Add(new LwPolylineVertex(10, -10)); poly.Vertexes.Add(new LwPolylineVertex(10, 10)); poly.Vertexes.Add(new LwPolylineVertex(-10, 10)); poly.Vertexes[2].Bulge = 1; poly.IsClosed = true; LwPolyline poly2 = new LwPolyline(); poly2.Vertexes.Add(new LwPolylineVertex(-5, -5)); poly2.Vertexes.Add(new LwPolylineVertex(5, -5)); poly2.Vertexes.Add(new LwPolylineVertex(5, 5)); poly2.Vertexes.Add(new LwPolylineVertex(-5, 5)); poly2.Vertexes[1].Bulge = -0.25; poly2.IsClosed = true; LwPolyline poly3 = new LwPolyline(); poly3.Vertexes.Add(new LwPolylineVertex(-8, -8)); poly3.Vertexes.Add(new LwPolylineVertex(-6, -8)); poly3.Vertexes.Add(new LwPolylineVertex(-6, -6)); poly3.Vertexes.Add(new LwPolylineVertex(-8, -6)); poly3.IsClosed = true; List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>{ new HatchBoundaryPath(new List<EntityObject>{poly}), new HatchBoundaryPath(new List<EntityObject>{poly2}), new HatchBoundaryPath(new List<EntityObject>{poly3}), }; HatchPattern pattern = new HatchPattern("MyPattern", "A custom hatch pattern"); HatchPatternLineDefinition line1 = new HatchPatternLineDefinition(); line1.Angle = 45; line1.Origin = Vector2.Zero; line1.Delta=new Vector2(4,4); line1.DashPattern.Add(12); line1.DashPattern.Add(-4); pattern.LineDefinitions.Add(line1); HatchPatternLineDefinition line2 = new HatchPatternLineDefinition(); line2.Angle = 135; line2.Origin = new Vector2(2.828427125, 2.828427125); line2.Delta = new Vector2(4,-4); line2.DashPattern.Add(12); line2.DashPattern.Add(-4); pattern.LineDefinitions.Add(line2); Hatch hatch = new Hatch(pattern, boundary, true); hatch.Layer = new Layer("hatch") { Color = AciColor.Red, LineType = LineType.Continuous }; hatch.Pattern.Angle = 0; hatch.Pattern.Scale = 1; dxf.AddEntity(poly); dxf.AddEntity(poly2); dxf.AddEntity(poly3); dxf.AddEntity(hatch); dxf.Save("hatchTest.dxf"); }