예제 #1
0
        public ApplicationPlaceholderObject AdaptiveComponentToNative(AdaptiveComponent speckleAc)
        {
            var docObj = GetExistingElementByApplicationId(speckleAc.applicationId);

            string familyName = speckleAc["family"] as string != null ? speckleAc["family"] as string : "";

            DB.FamilySymbol   familySymbol = GetElementType <DB.FamilySymbol>(speckleAc);
            DB.FamilyInstance revitAc      = null;

            //try update existing
            if (docObj != null)
            {
                try
                {
                    var revitType = Doc.GetElement(docObj.GetTypeId()) as ElementType;

                    // if family changed, tough luck. delete and let us create a new one.
                    if (familyName != revitType.FamilyName)
                    {
                        Doc.Delete(docObj.Id);
                    }
                    else
                    {
                        revitAc = (DB.FamilyInstance)docObj;

                        // check for a type change
                        if (speckleAc.type != null && speckleAc.type != revitType.Name)
                        {
                            revitAc.ChangeTypeId(familySymbol.Id);
                        }
                    }
                }
                catch
                {
                    //something went wrong, re-create it
                }
            }

            //create family instance
            if (revitAc == null)
            {
                revitAc = AdaptiveComponentInstanceUtils.CreateAdaptiveComponentInstance(Doc, familySymbol);
            }

            SetAdaptivePoints(revitAc, speckleAc.basePoints);
            AdaptiveComponentInstanceUtils.SetInstanceFlipped(revitAc, speckleAc.flipped);

            SetInstanceParameters(revitAc, speckleAc);

            return(new ApplicationPlaceholderObject {
                applicationId = speckleAc.applicationId, ApplicationGeneratedId = revitAc.UniqueId, NativeObject = revitAc
            });
        }
예제 #2
0
        public static Revit.Elements.FamilyInstance ChangeType(
            Revit.Elements.FamilyInstance familyInstance,
            Revit.Elements.FamilyType familyType)
        {
            // Unwrap input parameters
            Autodesk.Revit.DB.FamilyInstance instance    = familyInstance.InternalElement as Autodesk.Revit.DB.FamilyInstance;
            Autodesk.Revit.DB.Element        anotherType = familyType.InternalElement;

            TransactionManager.Instance.EnsureInTransaction(DocumentManager.Instance.CurrentDBDocument);
            instance.ChangeTypeId(anotherType.Id);
            TransactionManager.Instance.TransactionTaskDone();

            return(familyInstance);
        }
예제 #3
0
        //TODO: might need to clean this up and split the ConversionLog.Addic by beam, FI, etc...
        public List <ApplicationPlaceholderObject> FamilyInstanceToNative(BuiltElements.Revit.FamilyInstance speckleFi)
        {
            DB.FamilySymbol familySymbol = GetElementType <FamilySymbol>(speckleFi);
            XYZ             basePoint    = PointToNative(speckleFi.basePoint);

            DB.Level          level          = LevelToNative(speckleFi.level);
            DB.FamilyInstance familyInstance = null;
            var isUpdate = false;
            //try update existing
            var docObj = GetExistingElementByApplicationId(speckleFi.applicationId);

            if (docObj != null)
            {
                try
                {
                    var revitType = Doc.GetElement(docObj.GetTypeId()) as ElementType;

                    // if family changed, tough luck. delete and let us create a new one.
                    if (familySymbol.FamilyName != revitType.FamilyName)
                    {
                        Doc.Delete(docObj.Id);
                    }
                    else
                    {
                        familyInstance = (DB.FamilyInstance)docObj;

                        //NOTE: updating an element location is quite buggy in Revit!
                        //Let's say the first time an element is created its base point/curve is @ 10m and the Level is @ 0m
                        //the element will be created @ 0m
                        //but when this element is updated (let's say with no changes), it will jump @ 10m (unless there is a level change)!
                        //to avoid this behavior we're always setting the previous location Z coordinate when updating an element
                        //this means the Z coord of an element will only be set by its Level
                        //and by additional parameters as sill height, base offset etc
                        (familyInstance.Location as LocationPoint).Point = new XYZ(basePoint.X, basePoint.Y, (familyInstance.Location as LocationPoint).Point.Z);

                        // check for a type change
                        if (speckleFi.type != null && speckleFi.type != revitType.Name)
                        {
                            familyInstance.ChangeTypeId(familySymbol.Id);
                        }

                        TrySetParam(familyInstance, BuiltInParameter.FAMILY_LEVEL_PARAM, level);
                        TrySetParam(familyInstance, BuiltInParameter.FAMILY_BASE_LEVEL_PARAM, level);
                    }
                    isUpdate = true;
                }
                catch
                {
                    //something went wrong, re-create it
                }
            }

            //create family instance
            if (familyInstance == null)
            {
                //If the current host element is not null, it means we're coming from inside a nested conversion.
                if (CurrentHostElement != null)
                {
                    familyInstance = Doc.Create.NewFamilyInstance(basePoint, familySymbol, CurrentHostElement, level, StructuralType.NonStructural);
                }
                //Otherwise, proceed as normal.
                else
                {
                    familyInstance = Doc.Create.NewFamilyInstance(basePoint, familySymbol, level, StructuralType.NonStructural);
                }
            }

            //required for face flipping to work!
            Doc.Regenerate();

            if (familyInstance.CanFlipHand && speckleFi.handFlipped != familyInstance.HandFlipped)
            {
                familyInstance.flipHand();
            }

            if (familyInstance.CanFlipFacing && speckleFi.facingFlipped != familyInstance.FacingFlipped)
            {
                familyInstance.flipFacing();
            }

            // NOTE: do not check for the CanRotate prop as it doesn't work (at least on some families I tried)!
            // some point based families don't have a rotation, so keep this in a try catch
            try
            {
                if (speckleFi.rotation != (familyInstance.Location as LocationPoint).Rotation)
                {
                    var axis = DB.Line.CreateBound(new XYZ(basePoint.X, basePoint.Y, 0), new XYZ(basePoint.X, basePoint.Y, 1000));
                    (familyInstance.Location as LocationPoint).Rotate(axis, speckleFi.rotation - (familyInstance.Location as LocationPoint).Rotation);
                }
            }
            catch { }

            SetInstanceParameters(familyInstance, speckleFi);

            var placeholders = new List <ApplicationPlaceholderObject>()
            {
                new ApplicationPlaceholderObject
                {
                    applicationId          = speckleFi.applicationId,
                    ApplicationGeneratedId = familyInstance.UniqueId,
                    NativeObject           = familyInstance
                }
            };

            Report.Log($"{(isUpdate ? "Updated" : "Created")} FamilyInstance ({familyInstance.Category.Name}) {familyInstance.Id}");
            return(placeholders);
        }
예제 #4
0
        public List <ApplicationPlaceholderObject> ColumnToNative(Column speckleColumn)
        {
            if (speckleColumn.baseLine == null)
            {
                throw new Speckle.Core.Logging.SpeckleException("Only line based Beams are currently supported.");
            }

            DB.FamilySymbol familySymbol = GetElementType <FamilySymbol>(speckleColumn);
            var             baseLine     = CurveToNative(speckleColumn.baseLine).get_Item(0);

            // If the start point elevation is higher than the end point elevation, reverse the line.
            if (baseLine.GetEndPoint(0).Z > baseLine.GetEndPoint(1).Z)
            {
                baseLine = DB.Line.CreateBound(baseLine.GetEndPoint(1), baseLine.GetEndPoint(0));
            }

            DB.Level          level       = null;
            DB.Level          topLevel    = null;
            DB.FamilyInstance revitColumn = null;
            //var structuralType = StructuralType.Column;
            var isLineBased = true;

            var speckleRevitColumn = speckleColumn as RevitColumn;

            if (speckleRevitColumn != null)
            {
                level    = LevelToNative(speckleRevitColumn.level);
                topLevel = LevelToNative(speckleRevitColumn.topLevel);
                //structuralType = speckleRevitColumn.structural ? StructuralType.Column : StructuralType.NonStructural;
                //non slanted columns are point based
                isLineBased = speckleRevitColumn.isSlanted;
            }

            if (level == null)
            {
                level    = LevelToNative(LevelFromCurve(baseLine));
                topLevel = LevelToNative(LevelFromPoint(baseLine.GetEndPoint(1)));
            }

            //try update existing
            var  docObj   = GetExistingElementByApplicationId(speckleColumn.applicationId);
            bool isUpdate = false;

            if (docObj != null)
            {
                try
                {
                    var revitType = Doc.GetElement(docObj.GetTypeId()) as ElementType;

                    // if family changed, tough luck. delete and let us create a new one.
                    if (familySymbol.FamilyName != revitType.FamilyName)
                    {
                        Doc.Delete(docObj.Id);
                    }
                    else
                    {
                        revitColumn = (DB.FamilyInstance)docObj;
                        switch (revitColumn.Location)
                        {
                        case LocationCurve crv:
                            crv.Curve = baseLine;
                            break;

                        case LocationPoint pt:
                            pt.Point = baseLine.GetEndPoint(0);
                            break;
                        }

                        // check for a type change
                        if (!string.IsNullOrEmpty(familySymbol.FamilyName) && familySymbol.FamilyName != revitType.Name)
                        {
                            revitColumn.ChangeTypeId(familySymbol.Id);
                        }
                    }
                    isUpdate = true;
                }
                catch { }
            }

            if (revitColumn == null && isLineBased)
            {
                revitColumn = Doc.Create.NewFamilyInstance(baseLine, familySymbol, level, StructuralType.Column);
                if (revitColumn.Symbol.Family.FamilyPlacementType == FamilyPlacementType.CurveDrivenStructural)
                {
                    StructuralFramingUtils.DisallowJoinAtEnd(revitColumn, 0);
                    StructuralFramingUtils.DisallowJoinAtEnd(revitColumn, 1);
                }
            }

            //try with a point based column
            if (speckleRevitColumn != null && revitColumn == null && !isLineBased)
            {
                var start = baseLine.GetEndPoint(0);
                var end   = baseLine.GetEndPoint(1);

                var basePoint = start.Z < end.Z ? start : end; // pick the lowest
                revitColumn = Doc.Create.NewFamilyInstance(basePoint, familySymbol, level, StructuralType.NonStructural);
                //
                //rotate, we know it must be a RevitColumn
                var axis = DB.Line.CreateBound(new XYZ(basePoint.X, basePoint.Y, 0), new XYZ(basePoint.X, basePoint.Y, 1000));
                (revitColumn.Location as LocationPoint).Rotate(axis, speckleRevitColumn.rotation - (revitColumn.Location as LocationPoint).Rotation);
            }

            if (revitColumn == null)
            {
                throw (new Exception($"Failed to create column for {speckleColumn.applicationId}."));
            }

            TrySetParam(revitColumn, BuiltInParameter.FAMILY_BASE_LEVEL_PARAM, level);
            TrySetParam(revitColumn, BuiltInParameter.FAMILY_TOP_LEVEL_PARAM, topLevel);


            if (speckleRevitColumn != null)
            {
                if (speckleRevitColumn.handFlipped != revitColumn.HandFlipped)
                {
                    revitColumn.flipHand();
                }

                if (speckleRevitColumn.facingFlipped != revitColumn.FacingFlipped)
                {
                    revitColumn.flipFacing();
                }

                //do change offset for slanted columns, it's automatic
                if (!isLineBased)
                {
                    SetOffsets(revitColumn, speckleRevitColumn);
                }

                SetInstanceParameters(revitColumn, speckleRevitColumn);
            }

            var placeholders = new List <ApplicationPlaceholderObject>()
            {
                new ApplicationPlaceholderObject {
                    applicationId = speckleColumn.applicationId, ApplicationGeneratedId = revitColumn.UniqueId, NativeObject = revitColumn
                }
            };

            // TODO: nested elements.
            Report.Log($"{(isUpdate ? "Updated" : "Created")} Column {revitColumn.Id}");
            return(placeholders);
        }
예제 #5
0
        public List <ApplicationPlaceholderObject> BeamToNative(Beam speckleBeam, StructuralType structuralType = StructuralType.Beam)
        {
            if (speckleBeam.baseLine == null)
            {
                throw new Speckle.Core.Logging.SpeckleException("Only line based Beams are currently supported.");
            }

            DB.FamilySymbol familySymbol = GetElementType <FamilySymbol>(speckleBeam);
            var             baseLine     = CurveToNative(speckleBeam.baseLine).get_Item(0);

            DB.Level          level     = null;
            DB.FamilyInstance revitBeam = null;

            //comes from revit or schema builder, has these props
            var speckleRevitBeam = speckleBeam as RevitBeam;

            if (speckleRevitBeam != null)
            {
                if (level != null)
                {
                    level = GetLevelByName(speckleRevitBeam.level.name);
                }
            }

            if (level == null)
            {
                level = LevelToNative(LevelFromCurve(baseLine));
            }
            var isUpdate = false;
            //try update existing
            var docObj = GetExistingElementByApplicationId(speckleBeam.applicationId);

            if (docObj != null)
            {
                try
                {
                    var revitType = Doc.GetElement(docObj.GetTypeId()) as ElementType;

                    // if family changed, tough luck. delete and let us create a new one.
                    if (familySymbol.FamilyName != revitType.FamilyName)
                    {
                        Doc.Delete(docObj.Id);
                    }
                    else
                    {
                        revitBeam = (DB.FamilyInstance)docObj;
                        (revitBeam.Location as LocationCurve).Curve = baseLine;

                        // check for a type change
                        if (!string.IsNullOrEmpty(familySymbol.FamilyName) && familySymbol.FamilyName != revitType.Name)
                        {
                            revitBeam.ChangeTypeId(familySymbol.Id);
                        }
                    }
                    isUpdate = true;
                }
                catch
                {
                    //something went wrong, re-create it
                }
            }

            //create family instance
            if (revitBeam == null)
            {
                revitBeam = Doc.Create.NewFamilyInstance(baseLine, familySymbol, level, structuralType);
                StructuralFramingUtils.DisallowJoinAtEnd(revitBeam, 0);
                StructuralFramingUtils.DisallowJoinAtEnd(revitBeam, 1);
            }

            //reference level, only for beams
            TrySetParam(revitBeam, BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM, level);

            if (speckleRevitBeam != null)
            {
                SetInstanceParameters(revitBeam, speckleRevitBeam);
            }

            // TODO: get sub families, it's a family!
            var placeholders = new List <ApplicationPlaceholderObject>()
            {
                new ApplicationPlaceholderObject {
                    applicationId = speckleBeam.applicationId, ApplicationGeneratedId = revitBeam.UniqueId, NativeObject = revitBeam
                }
            };

            // TODO: nested elements.

            Report.Log($"{(isUpdate ? "Updated" : "Created")} AdaptiveComponent {revitBeam.Id}");

            return(placeholders);
        }