Example #1
0
        // Blocks
        public BlockInstance BlockReferenceToSpeckle(BlockReference reference)
        {
            /*
             * // skip if dynamic block
             * if (reference.IsDynamicBlock)
             * return null;
             */

            // get record
            BlockDefinition definition = null;
            var             attributes = new Dictionary <string, string>();

            BlockTableRecord btr = (BlockTableRecord)Trans.GetObject(reference.BlockTableRecord, OpenMode.ForRead);

            definition = BlockRecordToSpeckle(btr);
            foreach (ObjectId id in reference.AttributeCollection)
            {
                AttributeReference attRef = (AttributeReference)Trans.GetObject(id, OpenMode.ForRead);
                attributes.Add(attRef.Tag, attRef.TextString);
            }

            if (definition == null)
            {
                return(null);
            }

            var instance = new BlockInstance()
            {
                transform       = new Transform(reference.BlockTransform.ToArray(), ModelUnits),
                blockDefinition = definition,
                units           = ModelUnits
            };

            // add attributes
            instance["attributes"] = attributes;

            return(instance);
        }
Example #2
0
        public BlockDefinition BlockRecordToSpeckle(BlockTableRecord record)
        {
            // skip if this is from an external reference
            if (record.IsFromExternalReference)
            {
                return(null);
            }

            // get geometry
            var geometry = new List <Base>();

            foreach (ObjectId id in record)
            {
                DBObject obj       = Trans.GetObject(id, OpenMode.ForRead);
                Entity   objEntity = obj as Entity;
                if (CanConvertToSpeckle(obj))
                {
                    Base converted = ConvertToSpeckle(obj);
                    if (converted != null)
                    {
                        converted["Layer"] = objEntity.Layer;
                        geometry.Add(converted);
                    }
                }
            }

            var definition = new BlockDefinition()
            {
                name      = record.Name,
                basePoint = PointToSpeckle(record.Origin),
                geometry  = geometry,
                units     = ModelUnits
            };

            return(definition);
        }
Example #3
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);
        }