Example #1
0
        public static object TryConvertItemToSpeckle(object value, ISpeckleConverter converter)
        {
            object result = null;

            if (value is null)
            {
                throw new Speckle.Core.Logging.SpeckleException("Null values are not supported, please clean your data tree.");
            }

            if (value is IGH_Goo)
            {
                value = value.GetType().GetProperty("Value").GetValue(value);
            }

            if (value is Base || value.GetType().IsSimpleType())
            {
                return(value);
            }

            if (converter.CanConvertToSpeckle(value))
            {
                return(converter.ConvertToSpeckle(value));
            }



            return(result);
        }
Example #2
0
        /// <summary>
        /// Try to convert a given object into native (Rhino) format.
        /// </summary>
        /// <param name="value">Object to convert</param>
        /// <param name="converter">Converter instance to use.</param>
        /// <param name="recursive">Indicates if any non-convertible <see cref="Base"/> instances should be traversed too.</param>
        /// <returns>An <see cref="IGH_Goo"/> instance holding the converted object. </returns>
        public static object TryConvertItemToSpeckle(object value, ISpeckleConverter converter, bool recursive = false, Action OnConversionProgress = null)
        {
            if (value is null)
            {
                throw new Exception("Null values are not allowed, please clean your data tree.");
            }

            if (value is IGH_Goo)
            {
                value = value.GetType().GetProperty("Value").GetValue(value);
            }

            if (value.GetType().IsSimpleType())
            {
                return(value);
            }


            if (converter.CanConvertToSpeckle(value))
            {
                return(converter.ConvertToSpeckle(value));
            }

            var subclass = value.GetType().IsSubclassOf(typeof(Base));

            if (subclass)
            {
                // TODO: Traverse through dynamic props only.
                return(value);
            }

            if (recursive && value is Base @base)
            {
                return(TraverseAndConvertToSpeckle(@base, converter));
            }

            if (value is Base @base2)
            {
                return(@base2);
            }

            return(null);
        }
Example #3
0
        /// <summary>
        /// Gets the handles of all visible document objects that can be converted to Speckle
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="converter"></param>
        /// <returns></returns>
        public static List <string> ConvertibleObjects(this Document doc, ISpeckleConverter converter)
        {
            var objs = new List <string>();

            using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
            {
                BlockTable       blckTbl     = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord blckTblRcrd = tr.GetObject(blckTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
                foreach (ObjectId id in blckTblRcrd)
                {
                    DBObject dbObj = tr.GetObject(id, OpenMode.ForRead);
                    if (converter.CanConvertToSpeckle(dbObj) && dbObj.Visible())
                    {
                        objs.Add(dbObj.Handle.ToString());
                    }
                }
                tr.Commit();
            }
            return(objs);
        }
Example #4
0
        public static object TryConvertItemToSpeckle(object value, ISpeckleConverter converter)
        {
            object result = null;

            if (value is IGH_Goo)
            {
                value = value.GetType().GetProperty("Value").GetValue(value);
            }

            if (value is Base || value.GetType().IsSimpleType())
            {
                return(value);
            }

            if (converter.CanConvertToSpeckle(value))
            {
                return(converter.ConvertToSpeckle(value));
            }



            return(result);
        }
Example #5
0
        /// <summary>
        /// Gets the ids of all visible model objects that can be converted to Speckle
        /// </summary>
        /// <param name="model"></param>
        /// <param name="converter"></param>
        /// <returns></returns>
        public static List <string> ConvertibleObjects(this DgnModel model, ISpeckleConverter converter)
        {
            var objs = new List <string>();

            if (model == null)
            {
                return(new List <string>());
            }

            var graphicElements   = model.GetGraphicElements();
            var elementEnumerator = (ModelElementsEnumerator)graphicElements.GetEnumerator();
            var elements          = graphicElements.Where(el => !el.IsInvisible).Select(el => el).ToList();

            foreach (var element in elements)
            {
                if (converter.CanConvertToSpeckle(element) && !element.IsInvisible)
                {
                    objs.Add(element.ElementId.ToString());
                }
            }

            objs = graphicElements.Where(el => !el.IsInvisible).Select(el => el.ElementId.ToString()).ToList();
            return(objs);
        }