Exemple #1
0
        public Hatch(HatchPattern pattern, IEnumerable <HatchBoundaryPath> paths, bool associative)
            : base(EntityType.Hatch, DxfObjectCode.Hatch)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }
            this.pattern = pattern;

            if (paths == null)
            {
                throw new ArgumentNullException(nameof(paths));
            }
            this.boundaryPaths = new ObservableCollection <HatchBoundaryPath>();
            this.boundaryPaths.BeforeAddItem    += this.BoundaryPaths_BeforeAddItem;
            this.boundaryPaths.AddItem          += this.BoundaryPaths_AddItem;
            this.boundaryPaths.BeforeRemoveItem += this.BoundaryPaths_BeforeRemoveItem;
            this.boundaryPaths.RemoveItem       += this.BoundaryPaths_RemoveItem;
            foreach (HatchBoundaryPath path in paths)
            {
                if (!associative)
                {
                    path.ClearContour();
                }
                this.boundaryPaths.Add(path);
            }

            this.associative = associative;
        }
Exemple #2
0
        public virtual object Clone()
        {
            HatchPattern copy = new HatchPattern(this.name, this.description)
            {
                Style  = this.style,
                Fill   = this.fill,
                Type   = this.type,
                Origin = this.origin,
                Angle  = this.angle,
                Scale  = this.scale,
            };

            foreach (HatchPatternLineDefinition def in this.lineDefinitions)
            {
                copy.LineDefinitions.Add((HatchPatternLineDefinition)def.Clone());
            }

            return(copy);
        }
Exemple #3
0
        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);
        }