Exemple #1
0
        private static DB.ElementId[] DirectShapeElement(Element hyparElement, LoadContext context, string name, DB.ElementId categoryId)
        {
            if (!GeometryLoaderConfig.ShouldDirectShapeObject(hyparElement) ||
                (hyparElement is GeometricElement geometricElement && geometricElement.IsElementDefinition))
            {
                return(Array.Empty <DB.ElementId>());
            }

            var ds           = DB.DirectShape.CreateElement(context.Document, categoryId);
            var shapeBuilder = hyparElement.GetShapeBuilder(context.Document);

            if (shapeBuilder == null)
            {
                return(Array.Empty <DB.ElementId>());
            }

            ds.SetShape(shapeBuilder);
            ds.LookupParameter("Comments")?.Set(name);
            ds.LookupParameter("Mark")?.Set(hyparElement.Id.ToString());
            return(new[] { ds.Id });
        }
Exemple #2
0
        private DB.ElementId[] DirectShapeInstance(LoadContext context, DB.ElementId categoryId, ElementInstance instance)
        {
            var discriminator = "ElementInstance";

            if (instance.BaseDefinition.AdditionalProperties.TryGetValue("discriminator", out var discriminatorValue) && discriminatorValue != null)
            {
                discriminator += $" of {discriminatorValue as string}";
            }
            if (!GeometryLoaderConfig.ShouldDirectShapeObject(instance.BaseDefinition))
            {
                return(Array.Empty <DB.ElementId>());
            }
            else
            {
                var defTypeId = DB.DirectShapeLibrary.GetDirectShapeLibrary(context.Document).FindDefinitionType(instance.BaseDefinition.Id.ToString());
                // If a user uses "Undo" after we've created some direct shape objects the DirectShapeLibrary
                // does not refresh and know that this element has been removed.  This will look like it's going
                // to work all the way until we go to regenerate the document, when a DocumentCorruption error
                // will be posted resulting in the entire Load operation (all converters) to be rolled back.
                var foundType = context.Document.GetElement(defTypeId);
                if (defTypeId == DB.ElementId.InvalidElementId || foundType == null)
                {
                    string typeName     = TypeNameForDirectShapeType(instance);
                    var    defType      = DB.DirectShapeType.Create(context.Document, typeName, categoryId);
                    var    shapeBuilder = instance.BaseDefinition.GetShapeBuilder(context.Document);
                    if (shapeBuilder != null)
                    {
                        defType.AppendShape(shapeBuilder);
                    }
                    else
                    {
                        return(Array.Empty <DB.ElementId>());
                    }
                    defTypeId = defType.Id;
                    DB.DirectShapeLibrary.GetDirectShapeLibrary(context.Document).AddDefinitionType(instance.BaseDefinition.Id.ToString(), defTypeId);
                }

                var completeTransform = new Transform();
                if (instance.BaseDefinition.Transform != null)
                {
                    completeTransform.Concatenate(instance.BaseDefinition.Transform);
                    completeTransform.Invert();
                }
                if (instance.Transform != null)
                {
                    completeTransform.Concatenate(instance.Transform);
                }

                var directShape = DB.DirectShape.CreateElementInstance(context.Document,
                                                                       defTypeId,
                                                                       categoryId,
                                                                       instance.BaseDefinition.Id.ToString(),
                                                                       completeTransform.ToRevitTransform(true));
                if (directShape == null)
                {
                    return(Array.Empty <DB.ElementId>());
                }

                directShape.LookupParameter("Comments")?.Set(discriminator);
                directShape.LookupParameter("Mark")?.Set(instance.Id.ToString());
                return(new[] { directShape.Id });
            }
        }