Esempio n. 1
0
        public Hatch HatchToSpeckle(AcadDB.Hatch hatch)
        {
            var _hatch = new Hatch();

            _hatch.pattern  = hatch.PatternName;
            _hatch.scale    = hatch.PatternScale;
            _hatch.rotation = hatch.PatternAngle;

            // handle curves
            var curves = new List <HatchLoop>();

            for (int i = 0; i < hatch.NumberOfLoops; i++)
            {
                var loop = hatch.GetLoopAt(i);
                if (loop.IsPolyline)
                {
                    var poly          = GetPolylineFromBulgeVertexCollection(loop.Polyline);
                    var convertedPoly = poly.IsOnlyLines ? PolylineToSpeckle(poly) : PolycurveToSpeckle(poly);
                    var speckleLoop   = new HatchLoop(convertedPoly, HatchLoopTypeToSpeckle(loop.LoopType));
                    curves.Add(speckleLoop);
                }
                else
                {
                    for (int j = 0; j < loop.Curves.Count; j++)
                    {
                        var convertedCurve = CurveToSpeckle(loop.Curves[j]);
                        var speckleLoop    = new HatchLoop(convertedCurve, HatchLoopTypeToSpeckle(loop.LoopType));
                        curves.Add(speckleLoop);
                    }
                }
            }
            _hatch.loops    = curves;
            _hatch["style"] = hatch.HatchStyle.ToString();

            return(_hatch);
        }
Esempio n. 2
0
        // TODO: this needs to be improved, hatch curves not being created with HatchLoopTypes.Polyline flag
        public AcadDB.Hatch HatchToNativeDB(Hatch hatch)
        {
            BlockTableRecord modelSpaceRecord = Doc.Database.GetModelSpace();

            // convert curves
            var loops = new Dictionary <AcadDB.Curve, HatchLoopTypes>();

            if (hatch.loops != null)
            {
                foreach (var loop in hatch.loops)
                {
                    var converted = CurveToNativeDB(loop.Curve);
                    if (converted == null)
                    {
                        continue;
                    }

                    var curveId = modelSpaceRecord.Append(converted);
                    if (curveId.IsValid)
                    {
                        HatchLoopTypes type = HatchLoopTypeToNative(loop.Type);
                        loops.Add(converted, type);
                    }
                }
            }
            else // this is just here for backwards compatibility, before loops were introduced. Deprecate a few releases after 2.2.6
            {
                foreach (var loop in hatch.curves)
                {
                    var converted = CurveToNativeDB(loop);
                    if (converted == null)
                    {
                        continue;
                    }

                    var curveId = modelSpaceRecord.Append(converted);
                    if (curveId.IsValid)
                    {
                        loops.Add(converted, HatchLoopTypes.Default);
                    }
                }
            }
            if (loops.Count == 0)
            {
                return(null);
            }

            // add hatch to modelspace
            var _hatch = new AcadDB.Hatch();

            modelSpaceRecord.Append(_hatch);

            _hatch.SetDatabaseDefaults();
            // try get hatch pattern
            var patternCategory = HatchPatterns.ValidPatternName(hatch.pattern);

            switch (patternCategory)
            {
            case PatPatternCategory.kCustomdef:
                _hatch.SetHatchPattern(HatchPatternType.CustomDefined, hatch.pattern);
                break;

            case PatPatternCategory.kPredef:
            case PatPatternCategory.kISOdef:
                _hatch.SetHatchPattern(HatchPatternType.PreDefined, hatch.pattern);
                break;

            case PatPatternCategory.kUserdef:
                _hatch.SetHatchPattern(HatchPatternType.UserDefined, hatch.pattern);
                break;

            default:
                _hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
                break;
            }
            _hatch.PatternAngle = hatch.rotation;
            _hatch.PatternScale = hatch.scale;
            var style = hatch["style"] as string;

            if (style != null)
            {
                _hatch.HatchStyle = Enum.TryParse(style, out HatchStyle hatchStyle) ? hatchStyle : HatchStyle.Normal;
            }

            // create loops
            foreach (var entry in loops)
            {
                _hatch.AppendLoop(entry.Value, new ObjectIdCollection()
                {
                    entry.Key.ObjectId
                });
            }
            _hatch.EvaluateHatch(true);
            foreach (var entry in loops) // delete created hatch curves
            {
                entry.Key.Erase();
            }

            return(_hatch);
        }