Ejemplo n.º 1
0
        public ObjectId BlockDefinitionToNativeDB(BlockDefinition definition)
        {
            // get modified definition name with commit info
            var blockName = RemoveInvalidAutocadChars($"{Doc.UserData["commit"]} - {definition.name}");

            ObjectId blockId = ObjectId.Null;

            // see if block record already exists and return if so
            BlockTable blckTbl = Trans.GetObject(Doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;

            if (blckTbl.Has(blockName))
            {
                return(blckTbl[blockName]);
            }

            // create btr
            using (BlockTableRecord btr = new BlockTableRecord())
            {
                btr.Name = blockName;

                // base point
                btr.Origin = PointToNative(definition.basePoint);

                // add geometry
                blckTbl.UpgradeOpen();
                var bakedGeometry = new ObjectIdCollection(); // this is to contain block def geometry that is already added to doc space during conversion
                foreach (var geo in definition.geometry)
                {
                    if (CanConvertToNative(geo))
                    {
                        Entity converted = null;
                        switch (geo)
                        {
                        case BlockInstance o:
                            BlockInstanceToNativeDB(o, out BlockReference reference, false);
                            converted = reference;
                            break;

                        default:
                            converted = ConvertToNative(geo) as Entity;
                            break;
                        }

                        if (converted == null)
                        {
                            continue;
                        }
                        else if (!converted.IsNewObject && !(converted is BlockReference))
                        {
                            bakedGeometry.Add(converted.Id);
                        }
                        else
                        {
                            btr.AppendEntity(converted);
                        }
                    }
                }
                blockId = blckTbl.Add(btr);
                btr.AssumeOwnershipOf(bakedGeometry); // add in baked geo
                Trans.AddNewlyCreatedDBObject(btr, true);
                blckTbl.Dispose();
            }


            return(blockId);
        }