コード例 #1
0
        public string BlockInstanceToNativeDB(BlockInstance instance, out BlockReference reference, bool AppendToModelSpace = true)
        {
            string result = null;

            reference = null;

            // block definition
            ObjectId definitionId = BlockDefinitionToNativeDB(instance.blockDefinition);

            if (definitionId == ObjectId.Null)
            {
                return(result);
            }

            // insertion pt
            Point3d insertionPoint = PointToNative(instance.GetInsertionPoint());

            // transform
            double[] transform = instance.transform.value;
            for (int i = 3; i < 12; i += 4)
            {
                transform[i] = ScaleToNative(transform[i], instance.units);
            }
            Matrix3d convertedTransform = new Matrix3d(transform);

            // add block reference
            BlockTableRecord modelSpaceRecord = Doc.Database.GetModelSpace();
            BlockReference   br = new BlockReference(insertionPoint, definitionId);

            br.BlockTransform = convertedTransform;
            // add attributes if there are any
            var attributes = instance["attributes"] as Dictionary <string, string>;

            if (attributes != null)
            {
                // TODO: figure out how to add attributes
            }
            ObjectId id = ObjectId.Null;

            if (AppendToModelSpace)
            {
                id = modelSpaceRecord.Append(br);
            }

            // return
            result = "success";
            if ((id.IsValid && !id.IsNull) || !AppendToModelSpace)
            {
                reference = br;
            }

            return(result);
        }
コード例 #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);
        }