Example #1
0
        /// <summary>
        /// Mutate the name of the level
        /// </summary>
        /// <param name="name"> the name we want to set the level to have</param>
        /// <param name="oldname"> the oldname we tried to set this level to in the past,
        /// we retrieve this from trace</param>
        private void InternalSetName(string oldname, string name)
        {
            if (String.IsNullOrEmpty(name) ||
                string.CompareOrdinal(InternalLevel.Name, name) == 0 ||
                string.CompareOrdinal(oldname, name) == 0)
            {
                return;
            }
            //make a copy of the string
            var originalInputName = name;
            //Check to see whether there is an existing level with the same name
            var levels = ElementQueries.GetAllLevels();

            while (levels.Any(x => string.CompareOrdinal(x.Name, name) == 0))
            {
                //Update the level name
                Revit.Elements.InternalUtilities.ElementUtils.UpdateLevelName(ref name);
            }

            TransactionManager.Instance.EnsureInTransaction(Document);
            this.InternalLevel.Name = name;
            var updatedTraceData = new LevelTraceData(this, originalInputName);

            ElementBinder.SetRawDataForTrace(updatedTraceData);
            TransactionManager.Instance.TransactionTaskDone();
        }
Example #2
0
        /// <summary>
        /// Initialize a Level element
        /// </summary>
        /// <param name="elevation"></param>
        /// <param name="name"></param>
        private void InitLevel(double elevation, string name)
        {
            //Phase 1 - Check to see if the object exists and should be rebound
            var oldEle = ElementBinder.GetElementAndTraceData <Autodesk.Revit.DB.Level, LevelTraceData>(Document);

            //There was an element, bind & mutate
            if (oldEle != null)
            {
                InternalSetLevel(oldEle.Item1);
                InternalSetElevation(elevation);
                InternalSetName(oldEle.Item2.InputName, name);
                return;
            }

            //There was no element, create a new one
            TransactionManager.Instance.EnsureInTransaction(Document);

            Autodesk.Revit.DB.Level level;

            if (Document.IsFamilyDocument)
            {
                level = Autodesk.Revit.DB.Level.Create(Document, elevation);
            }
            else
            {
                level = Autodesk.Revit.DB.Level.Create(Document, elevation);
            }

            InternalSetLevel(level);
            InternalSetName(string.Empty, name);
            var traceData = new LevelTraceData(this, name);

            TransactionManager.Instance.TransactionTaskDone();

            ElementBinder.SetRawDataForTrace(traceData);
        }