Exemplo n.º 1
0
        public static LinkedInstanceProperties DuplicateSelectedCategories(LinkedInstanceProperties lip, Document recipientDoc, UpdateMode updateMode)
        {
            var updatedLIP = lip;

            try
            {
                messageBuilder = new StringBuilder();
                var updateLabelDelegate = new UpdateStatusLabelDelegate(statusLabel.SetValue);

                progressBar.Visibility = System.Windows.Visibility.Visible;
                var categories = lip.Categories.Values.ToList();
                //categories = categories.OrderBy(o => o.Priority).ToList();
                foreach (var cp in categories)
                {
                    if (cp.Selected)
                    {
                        System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(updateLabelDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { TextBlock.TextProperty, "Copying " + cp.CategoryName + "..." });
                        updatedLIP = DuplicateElements(recipientDoc, updatedLIP, cp, updateMode);
                    }
                }
                progressBar.Visibility = System.Windows.Visibility.Hidden;
                System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(updateLabelDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { TextBlock.TextProperty, "Ready" });

                MessageBox.Show(messageBuilder.ToString(), "Duplicate Elements", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to move elements.\n" + ex.Message, "Move Elements", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(updatedLIP);
        }
Exemplo n.º 2
0
 public void CollectRvtLinks()
 {
     try
     {
         var collector = new FilteredElementCollector(CurrentDocument);
         var instances = collector.OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType().Cast <RevitLinkInstance>().ToList();
         if (instances.Count > 0)
         {
             foreach (var instance in instances)
             {
                 var lip = new LinkedInstanceProperties(instance);
                 if (!LinkInstances.ContainsKey(lip.InstanceId))
                 {
                     LinkInstances.Add(lip.InstanceId, lip);
                 }
             }
         }
         else
         {
             MessageBox.Show("The current project doesn't contain any Revit link instances.", "Revit Link Instances Missing", MessageBoxButton.OK, MessageBoxImage.Warning);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Failed to collect Revit Link Instances.\n" + ex.Message, "Collect Revit Links", MessageBoxButton.OK, MessageBoxImage.Warning);
     }
 }
Exemplo n.º 3
0
        private bool PickLinkInstance(out LinkedInstanceProperties lip)
        {
            var picked = false;

            lip = null;
            using (var trans = new Transaction(CurrentDocument))
            {
                trans.Start("Pick Revit Link");
                try
                {
                    var selection = m_app.ActiveUIDocument.Selection;
                    ISelectionFilter selectFilter = new LinkInstanceSelectionFilter();
                    var reference = selection.PickObject(ObjectType.Element, selectFilter, "Select a Revit Link instance to retreive elements for source items.");
                    if (null != reference)
                    {
                        var elementId = reference.ElementId;
                        if (LinkInstances.ContainsKey(elementId))
                        {
                            lip    = LinkInstances[elementId];
                            picked = true;
                        }
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.RollBack();
                    MessageBox.Show("Failed to select Revit Link.\n" + ex.Message, "Pick Revit Link Instance", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            return(picked);
        }
Exemplo n.º 4
0
        public static List <TreeViewFamilyModel> SetTreeView(LinkedInstanceProperties lip)
        {
            var treeView = new List <TreeViewFamilyModel>();

            try
            {
                var categories = from linkedFamily in lip.LinkedFamilies.Values select linkedFamily.CategoryName;
                if (categories.Count() > 0)
                {
                    var categoryNames = categories.Distinct().ToList(); categoryNames.Sort();
                    foreach (var categoryName in categoryNames)
                    {
                        var categoryNode = new TreeViewFamilyModel(categoryName);
                        categoryNode.NodeType = TreeViewNodeType.Category;
                        treeView.Add(categoryNode);

                        var families = from linkedFamily in lip.LinkedFamilies.Values
                                       where linkedFamily.CategoryName == categoryName
                                       select linkedFamily.SourceFamilyName;
                        if (families.Count() > 0)
                        {
                            var familyNames = families.Distinct().ToList(); familyNames.Sort();
                            foreach (var familyName in familyNames)
                            {
                                var linkedFamilies = from linkedFamily in lip.LinkedFamilies.Values
                                                     where linkedFamily.CategoryName == categoryName && linkedFamily.SourceFamilyName == familyName
                                                     select linkedFamily;

                                if (linkedFamilies.Count() > 0)
                                {
                                    var familyInfo = linkedFamilies.First();

                                    var familyNode = new TreeViewFamilyModel(familyInfo.SourceFamilyName + " : " + familyInfo.TargetFamilyName);
                                    familyNode.NodeType = TreeViewNodeType.Family;
                                    categoryNode.ChildrenNodes.Add(familyNode);

                                    var familyTypes = linkedFamilies.OrderBy(o => o.SourceTypeName).ToList();
                                    foreach (var linkedInfo in familyTypes)
                                    {
                                        var familyTypeNode = new TreeViewFamilyModel(linkedInfo.SourceTypeName + " : " + linkedInfo.TargetTypeName);
                                        familyTypeNode.NodeType = TreeViewNodeType.FamilyType;
                                        familyTypeNode.Tag      = linkedInfo;
                                        familyNode.ChildrenNodes.Add(familyTypeNode);
                                    }
                                }
                            }
                        }
                        categoryNode.Initialize();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to set the tree view.\n" + ex.Message, "Set Tree View", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(treeView);
        }
Exemplo n.º 5
0
        private static FamilyInstance PlaceFamilyInstance(Document recipientDoc, LinkedInstanceProperties lip, Element sourceElement)
        {
            FamilyInstance placedInstance = null;

            try
            {
                var sourceInstance = sourceElement as FamilyInstance;
                if (null != sourceInstance)
                {
                    var sourceSymbol = sourceInstance.Symbol;
                    var foundSymbols = from symbol in lip.LinkedFamilies.Values
                                       where symbol.SourceLinkInstanceId == lip.InstanceId && symbol.SourceTypeId == sourceSymbol.Id
                                       select symbol;

                    if (foundSymbols.Count() > 0)
                    {
                        var linkedFamilyInfo = foundSymbols.First();
                        var targetSymbol     = recipientDoc.GetElement(linkedFamilyInfo.TargetTypeId) as FamilySymbol;
                        if (null != targetSymbol)
                        {
                            var location = sourceInstance.Location;
                            if (null != location)
                            {
                                if (location is LocationPoint)
                                {
                                    var locationPt = location as LocationPoint;
                                    var point      = locationPt.Point;
                                    point          = lip.TransformValue.OfPoint(point);
                                    placedInstance = recipientDoc.Create.NewFamilyInstance(point, targetSymbol, sourceInstance.StructuralType);
                                }
                                else if (location is LocationCurve)
                                {
                                    var locationCv = location as LocationCurve;
                                    var curve      = locationCv.Curve;
                                    curve = curve.CreateTransformed(lip.TransformValue);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to place family instances by defined maps.\n" + ex.Message, "Place Family Instance", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(placedInstance);
        }
Exemplo n.º 6
0
 private void comboBoxLinkModel_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     try
     {
         if (null != comboBoxLinkModel.SelectedItem)
         {
             selectedLink           = (LinkedInstanceProperties)comboBoxLinkModel.SelectedItem;
             m_handler.SelectedLink = selectedLink;
             var catList = selectedLink.Categories.Values.OrderBy(o => o.CategoryName).ToList();
             dataGridCategory.ItemsSource = catList;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Failed to select a linked model.\n" + ex.Message, "Link Model Selection Changed", MessageBoxButton.OK, MessageBoxImage.Warning);
     }
 }
        private void comboBoxLink_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (null != comboBoxLink.SelectedItem)
                {
                    selectedLink = (LinkedInstanceProperties)comboBoxLink.SelectedItem;
                    treeViewMapping.ItemsSource = null;
                    treeViewMapping.ItemsSource = TreeViewElementModel.SetTreeView(selectedLink);

                    treeViewFamilyMapping.ItemsSource = null;
                    treeViewFamilyMapping.ItemsSource = TreeViewFamilyModel.SetTreeView(selectedLink);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to select a Revit Link.\n" + ex.Message, "Select a Revit Link", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
Exemplo n.º 8
0
        public FamilyWindow(ExternalEvent extEvent, MoverHandler handler)
        {
            m_event   = extEvent;
            m_handler = handler;
            m_handler.FamilyWindowInstance = this;

            selectedLink = m_handler.SelectedLink;
            familyInfo   = m_handler.SelectedFamilyInfo;

            linkedDoc = selectedLink.LinkedDocument;
            hostDoc   = m_handler.CurrentDocument;
            CollectElementTypes();

            InitializeComponent();

            var categories = selectedLink.Categories.Values.OrderBy(o => o.CategoryName).ToList();

            comboBoxCategory.ItemsSource   = null;
            comboBoxCategory.ItemsSource   = categories;
            comboBoxCategory.SelectedIndex = 0;
        }
        public void RefreshLinkInstance()
        {
            try
            {
                linkInstances = m_handler.LinkInstances;
                selectedLink  = m_handler.SelectedLink;
                var instances = linkInstances.Values.OrderBy(o => o.DisplayName).ToList();
                comboBoxLink.ItemsSource = null;
                comboBoxLink.ItemsSource = instances;

                var selectedIndex = instances.FindIndex(o => o.InstanceId == selectedLink.InstanceId);
                if (selectedIndex > -1)
                {
                    comboBoxLink.SelectedIndex = selectedIndex;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to refresh Link Instance Info.\n" + ex.Message, "Refresh Link Instance", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
Exemplo n.º 10
0
        public MappingWindow(ExternalEvent extEvent, MoverHandler handler)
        {
            m_event   = extEvent;
            m_handler = handler;
            m_handler.MappingWindowInstance = this;

            linkInstances = m_handler.LinkInstances;
            selectedLink  = m_handler.SelectedLink;

            InitializeComponent();

            var instances = linkInstances.Values.OrderBy(o => o.DisplayName).ToList();

            comboBoxLink.ItemsSource       = instances;
            comboBoxLink.DisplayMemberPath = "DisplayName";
            var selectedIndex = instances.FindIndex(o => o.InstanceId == selectedLink.InstanceId);

            if (selectedIndex > -1)
            {
                comboBoxLink.SelectedIndex = selectedIndex;
            }
        }
Exemplo n.º 11
0
 public void DisplayCategories(LinkedInstanceProperties lip)
 {
     try
     {
         var instanceList = (List <LinkedInstanceProperties>)comboBoxLinkModel.ItemsSource;
         if (instanceList.Count > 0)
         {
             for (var i = 0; i < instanceList.Count; i++)
             {
                 if (instanceList[i].InstanceId == lip.InstanceId)
                 {
                     comboBoxLinkModel.SelectedIndex = i;
                     break;
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("Failed to display category information from the selected Revit Link.\n" + ex.Message, "Display Categories", MessageBoxButton.OK, MessageBoxImage.Warning);
     }
 }
Exemplo n.º 12
0
        public static List <TreeViewElementModel> SetTreeView(LinkedInstanceProperties lip)
        {
            var treeView = new List <TreeViewElementModel>();

            try
            {
                var customCategories = new string[] { "Rooms", "Levels", "Grids", "Scope Boxes" };

                var categories = from linkedElement in lip.LinkedElements.Values select linkedElement.CategoryName;
                if (categories.Count() > 0)
                {
                    var categoryNames = categories.Distinct().ToList(); categoryNames.Sort();
                    foreach (var categoryName in categoryNames)
                    {
                        var categoryNode = new TreeViewElementModel(categoryName);
                        categoryNode.NodeType          = TreeViewNodeType.Category;
                        categoryNode.ToolTipVisibility = Visibility.Hidden;
                        treeView.Add(categoryNode);

                        if (customCategories.Contains(categoryName))
                        {
                            var elements = from linkedElement in lip.LinkedElements.Values
                                           where linkedElement.CategoryName == categoryName
                                           select linkedElement;

                            var linkedElements = elements.OrderBy(o => o.LinkDisplayText).ToList();
                            foreach (var linkInfo in linkedElements)
                            {
                                var elementNode = new TreeViewElementModel(linkInfo.LinkDisplayText);
                                elementNode.Tag               = linkInfo;
                                elementNode.NodeType          = TreeViewNodeType.ElementMapping;
                                elementNode.Matched           = linkInfo.Matched;
                                elementNode.ToolTip           = linkInfo.ToolTipText;
                                elementNode.ToolTipVisibility = Visibility.Visible;

                                categoryNode.ChildrenNodes.Add(elementNode);
                            }
                            continue;
                        }

                        var families = from linkedElement in lip.LinkedElements.Values
                                       where linkedElement.CategoryName == categoryName
                                       select linkedElement.FamilyName;
                        if (families.Count() > 0)
                        {
                            var familyNames = families.Distinct().ToList(); familyNames.Sort();
                            foreach (var familyName in familyNames)
                            {
                                var familyNode = new TreeViewElementModel(familyName);
                                familyNode.NodeType          = TreeViewNodeType.Family;
                                familyNode.ToolTipVisibility = Visibility.Hidden;
                                categoryNode.ChildrenNodes.Add(familyNode);

                                var familyTypes = from linkedElement in lip.LinkedElements.Values
                                                  where linkedElement.CategoryName == categoryName && linkedElement.FamilyName == familyName
                                                  select linkedElement.FamilyTypeName;
                                if (familyTypes.Count() > 0)
                                {
                                    var familyTypeNames = familyTypes.Distinct().ToList(); familyTypeNames.Sort();
                                    foreach (var familyTypeName in familyTypeNames)
                                    {
                                        var familyTypeNode = new TreeViewElementModel(familyTypeName);
                                        familyTypeNode.NodeType          = TreeViewNodeType.FamilyType;
                                        familyTypeNode.ToolTipVisibility = Visibility.Hidden;
                                        familyNode.ChildrenNodes.Add(familyTypeNode);

                                        var mappings = from linkedElement in lip.LinkedElements.Values
                                                       where linkedElement.CategoryName == categoryName && linkedElement.FamilyName == familyName && linkedElement.FamilyTypeName == familyTypeName
                                                       select linkedElement;
                                        if (mappings.Count() > 0)
                                        {
                                            var linkedElements = mappings.OrderBy(o => o.LinkDisplayText).ToList();
                                            foreach (var linkInfo in linkedElements)
                                            {
                                                var elementNode = new TreeViewElementModel(linkInfo.LinkDisplayText);
                                                elementNode.Tag               = linkInfo;
                                                elementNode.NodeType          = TreeViewNodeType.ElementMapping;
                                                elementNode.Matched           = linkInfo.Matched;
                                                elementNode.ToolTip           = linkInfo.ToolTipText;
                                                elementNode.ToolTipVisibility = Visibility.Visible;

                                                familyTypeNode.ChildrenNodes.Add(elementNode);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        categoryNode.Initialize();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to set the tree view.\n" + ex.Message, "Set Tree View", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(treeView);
        }
Exemplo n.º 13
0
        private static LinkedInstanceProperties DuplicateElements(Document recipientDoc, LinkedInstanceProperties lip, CategoryProperties cp, UpdateMode updateMode)
        {
            var updatedLIP = lip;

            try
            {
                var collector      = new FilteredElementCollector(updatedLIP.LinkedDocument);
                var elementsToCopy = collector.OfCategoryId(cp.CategoryId).WhereElementIsNotElementType().ToElements().ToList();

                var updatePbDelegate = new UpdateProgressBarDelegate(progressBar.SetValue);
                progressBar.Value   = 0;
                progressBar.Maximum = elementsToCopy.Count;

                var duplicated = 0;
                using (var tGroup = new TransactionGroup(recipientDoc))
                {
                    tGroup.Start("Duplicate Elements");
                    tGroup.IsFailureHandlingForcedModal = false;
                    var failureHanlder = new FailureHandler();

                    try
                    {
                        var options = new CopyPasteOptions();
                        options.SetDuplicateTypeNamesHandler(new HideAndAcceptDuplicateTypeNamesHandler());

                        double value = 0;
                        foreach (var sourceElement in elementsToCopy) //elements from link
                        {
                            value++;
                            System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                            var foundElements = from element in updatedLIP.LinkedElements.Values where element.SourceUniqueId == sourceElement.UniqueId && element.SourceLinkInstanceId == updatedLIP.InstanceId select element;
                            if (foundElements.Count() > 0)
                            {
                                var linkInfo      = foundElements.First();
                                var linkedElement = recipientDoc.GetElement(linkInfo.LinkedElementId);
                                if (null != linkedElement)
                                {
                                    if (linkInfo.LinkElementType == LinkType.ByMap || updateMode == UpdateMode.UpdateLocationOnly)
                                    {
                                        if (!linkInfo.Matched)
                                        {
                                            using (var trans = new Transaction(recipientDoc))
                                            {
                                                trans.Start("Move Element");
                                                var failureOption = trans.GetFailureHandlingOptions();
                                                failureOption.SetForcedModalHandling(false);
                                                failureOption.SetFailuresPreprocessor(failureHanlder);
                                                failureOption.SetClearAfterRollback(true);
                                                trans.SetFailureHandlingOptions(failureOption);

                                                try
                                                {
                                                    var moved = LinkedElementInfo.MoveLocation(sourceElement, linkedElement, updatedLIP.TransformValue);
                                                    linkInfo.Matched = moved;
                                                    if (updatedLIP.LinkedElements.ContainsKey(linkedElement.Id))
                                                    {
                                                        updatedLIP.LinkedElements.Remove(linkedElement.Id);
                                                        updatedLIP.LinkedElements.Add(linkedElement.Id, linkInfo);
                                                    }
                                                    trans.Commit();
                                                    duplicated++;
                                                }
                                                catch (Exception ex)
                                                {
                                                    trans.RollBack();
                                                    var message = ex.Message;
                                                }
                                            }
                                        }
                                        continue;
                                    }
                                    else if (updateMode == UpdateMode.ReplaceElements)
                                    {
                                        using (var trans = new Transaction(recipientDoc))
                                        {
                                            trans.Start("Delete Element");
                                            var failureOption = trans.GetFailureHandlingOptions();
                                            failureOption.SetForcedModalHandling(false);
                                            failureOption.SetFailuresPreprocessor(failureHanlder);
                                            failureOption.SetClearAfterRollback(true);
                                            trans.SetFailureHandlingOptions(failureOption);

                                            try
                                            {
                                                var deletedIds = recipientDoc.Delete(linkInfo.LinkedElementId);
                                                if (updatedLIP.LinkedElements.ContainsKey(linkInfo.LinkedElementId))
                                                {
                                                    updatedLIP.LinkedElements.Remove(linkInfo.LinkedElementId);
                                                }
                                                trans.Commit();
                                            }
                                            catch (Exception ex)
                                            {
                                                var message = ex.Message;
                                                trans.RollBack();
                                            }
                                        }
                                    }
                                }
                            }

                            var elementIds = new List <ElementId>();
                            elementIds.Add(sourceElement.Id);
                            try
                            {
                                Element copiedElement = null;
                                var     linkType      = LinkType.None;
                                using (var trans = new Transaction(recipientDoc))
                                {
                                    trans.Start("Copy Element");
                                    var failureOption = trans.GetFailureHandlingOptions();
                                    failureOption.SetForcedModalHandling(false);
                                    failureOption.SetFailuresPreprocessor(failureHanlder);
                                    failureOption.SetClearAfterRollback(true);
                                    trans.SetFailureHandlingOptions(failureOption);

                                    try
                                    {
                                        copiedElement = CopyByFamilyMaps(recipientDoc, updatedLIP, sourceElement, options);
                                        if (null != copiedElement)
                                        {
                                            linkType = LinkType.ByMap;
                                        }
                                        else
                                        {
                                            linkType = LinkType.ByCopy;
                                            var copiedElementIds = ElementTransformUtils.CopyElements(updatedLIP.LinkedDocument, elementIds, recipientDoc, updatedLIP.TransformValue, options);
                                            if (copiedElementIds.Count > 0)
                                            {
                                                var copiedElementId = copiedElementIds.First();
                                                copiedElement = recipientDoc.GetElement(copiedElementId);
                                            }
                                        }
                                        trans.Commit();
                                    }
                                    catch (Exception ex)
                                    {
                                        trans.RollBack();
                                        var message = ex.Message;
                                    }
                                }

                                if (null != copiedElement)
                                {
                                    using (var trans = new Transaction(recipientDoc))
                                    {
                                        trans.Start("Update Link Info");
                                        var failureOption = trans.GetFailureHandlingOptions();
                                        failureOption.SetForcedModalHandling(false);
                                        failureOption.SetFailuresPreprocessor(failureHanlder);
                                        failureOption.SetClearAfterRollback(true);
                                        trans.SetFailureHandlingOptions(failureOption);

                                        try
                                        {
                                            var linkInfo = new LinkedElementInfo(linkType, sourceElement, copiedElement, updatedLIP.InstanceId, updatedLIP.TransformValue);
                                            if (!linkInfo.Matched)
                                            {
                                                var moved = LinkedElementInfo.MoveLocation(sourceElement, copiedElement, updatedLIP.TransformValue);
                                                linkInfo.Matched = moved;
                                            }
                                            var updated = MoverDataStorageUtil.UpdateLinkedElementInfo(linkInfo, copiedElement);
                                            if (!updatedLIP.LinkedElements.ContainsKey(linkInfo.LinkedElementId))
                                            {
                                                updatedLIP.LinkedElements.Add(linkInfo.LinkedElementId, linkInfo);
                                            }
                                            duplicated++;
                                            trans.Commit();
                                        }
                                        catch (Exception ex)
                                        {
                                            trans.RollBack();
                                            var message = ex.Message;
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                var message = ex.Message;
                            }
                        }


                        if (updatedLIP.Categories.ContainsKey(cp.CategoryId))
                        {
                            cp.Selected = false;
                            updatedLIP.Categories.Remove(cp.CategoryId);
                            updatedLIP.Categories.Add(cp.CategoryId, cp);
                        }

                        tGroup.Assimilate();
                    }
                    catch (Exception ex)
                    {
                        tGroup.RollBack();
                        MessageBox.Show("Failed to duplicate elements in the category " + cp.CategoryName + "\n" + ex.Message, "Duplicate Elements", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                }

                messageBuilder.AppendLine(duplicated.ToString() + " of " + elementsToCopy.Count.ToString() + " elements in " + cp.CategoryName + " has been successfully copied or updated.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to duplicate elements.\n" + ex.Message, "Duplicate Elements", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(updatedLIP);
        }
Exemplo n.º 14
0
        private static Element CopyByFamilyMaps(Document recipientDoc, LinkedInstanceProperties lip, Element sourceElement, CopyPasteOptions options)
        {
            Element copiedElement = null;

            try
            {
                var foundTypes = from sType in lip.LinkedFamilies.Values
                                 where sType.SourceLinkInstanceId == lip.InstanceId && sType.SourceTypeId == sourceElement.GetTypeId()
                                 select sType;
                if (foundTypes.Count() > 0)
                {
                    var familyInfo = foundTypes.First();
                    var targetType = recipientDoc.GetElement(familyInfo.TargetTypeId) as ElementType;
                    if (null != targetType)
                    {
                        if (sourceElement is FamilyInstance && targetType is FamilySymbol)
                        {
                            var sourceInstance = sourceElement as FamilyInstance;
                            var targetSymbol   = targetType as FamilySymbol;
                            if (!targetSymbol.IsActive)
                            {
                                targetSymbol.Activate();
                            }

                            var location = sourceInstance.Location;
                            if (null != location)
                            {
                                Element hostElement = null;
                                if (null != sourceInstance.Host)
                                {
                                    var hostFound = from host in lip.LinkedElements.Values
                                                    where host.SourceLinkInstanceId == lip.InstanceId && host.SourceElementId == sourceInstance.Host.Id
                                                    select host;

                                    if (hostFound.Count() > 0)
                                    {
                                        var linkedElementInfo = hostFound.First();
                                        hostElement = recipientDoc.GetElement(linkedElementInfo.LinkedElementId);
                                    }
                                }
                                Level hostLevel = null;
                                if (null != sourceInstance.LevelId)
                                {
                                    var levelFound = from level in lip.LinkedElements.Values
                                                     where level.SourceLinkInstanceId == lip.InstanceId && level.SourceElementId == sourceInstance.LevelId
                                                     select level;
                                    if (levelFound.Count() > 0)
                                    {
                                        var linkedElementInfo = levelFound.First();
                                        hostLevel = recipientDoc.GetElement(linkedElementInfo.LinkedElementId) as Level;
                                    }
                                }

                                if (location is LocationPoint)
                                {
                                    var locationPt = location as LocationPoint;
                                    var point      = locationPt.Point;
                                    point = lip.TransformValue.OfPoint(point);

                                    if (null != hostElement && null != hostLevel)
                                    {
                                        copiedElement = recipientDoc.Create.NewFamilyInstance(point, targetSymbol, hostElement, hostLevel, sourceInstance.StructuralType);
                                    }
                                    else if (null != hostElement)
                                    {
                                        copiedElement = recipientDoc.Create.NewFamilyInstance(point, targetSymbol, hostElement, sourceInstance.StructuralType);
                                    }
                                    else if (null != hostLevel)
                                    {
                                        copiedElement = recipientDoc.Create.NewFamilyInstance(point, targetSymbol, hostLevel, sourceInstance.StructuralType);
                                    }
                                    else
                                    {
                                        copiedElement = recipientDoc.Create.NewFamilyInstance(point, targetSymbol, sourceInstance.StructuralType);
                                    }
                                }
                                else if (location is LocationCurve)
                                {
                                    var locationCv = location as LocationCurve;
                                    var curve      = locationCv.Curve;
                                    curve = curve.CreateTransformed(lip.TransformValue);
                                    //check existing level mapping
                                    if (null != hostLevel)
                                    {
                                        copiedElement = recipientDoc.Create.NewFamilyInstance(curve, targetSymbol, hostLevel, sourceInstance.StructuralType);
                                    }
                                }
                            }
                        }
                        else
                        {
                            //System Families : Copy Element and change type
                            var elementIds = new List <ElementId>();
                            elementIds.Add(sourceElement.Id);
                            var copiedElementIds = ElementTransformUtils.CopyElements(lip.LinkedDocument, elementIds, recipientDoc, lip.TransformValue, options);
                            if (copiedElementIds.Count > 0)
                            {
                                var copiedElementId = copiedElementIds.First();
                                copiedElement = recipientDoc.GetElement(copiedElementId);
                                if (copiedElement.CanHaveTypeAssigned())
                                {
                                    copiedElement.ChangeTypeId(familyInfo.TargetTypeId);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to place elements by family maps.\n" + ex.Message, "Place Family Maps", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(copiedElement);
        }
Exemplo n.º 15
0
        public void Execute(UIApplication app)
        {
            try
            {
                CurrentDocument = app.ActiveUIDocument.Document;

                switch (MoverRequest.Take())
                {
                case RequestId.SelectLinkInstance:
                    MainWindowInstance.DozeOff();
                    var picked = PickLinkInstance(out selectedLink);
                    if (picked)
                    {
                        MainWindowInstance.DisplayCategories(selectedLink);
                    }
                    MainWindowInstance.WakeUp();
                    break;

                case RequestId.DuplicateElements:
                    MainWindowInstance.DozeOff();
                    try
                    {
                        selectedLink = ElementMoverUtil.DuplicateSelectedCategories(selectedLink, CurrentDocument, SelectedUpdateMode);
                        if (LinkInstances.ContainsKey(selectedLink.InstanceId))
                        {
                            LinkInstances.Remove(selectedLink.InstanceId);
                        }
                        LinkInstances.Add(selectedLink.InstanceId, selectedLink);
                        MainWindowInstance.UpdateLinkedInstanceProperties();
                    }
                    catch (Exception ex)
                    {
                        var message = ex.Message;
                    }
                    MainWindowInstance.WakeUp();
                    break;

                case RequestId.SelectMappingElements:
                    MappingWindowInstance.DozeOff();
                    Element sourceElement = null;
                    Element targetElement = null;
                    var     pickedMap     = PickMappingElements(out sourceElement, out targetElement);
                    if (pickedMap)
                    {
                        if (LinkInstances.ContainsKey(selectedLink.InstanceId))
                        {
                            LinkInstances.Remove(selectedLink.InstanceId);
                        }
                        LinkInstances.Add(selectedLink.InstanceId, selectedLink);
                        MappingWindowInstance.RefreshLinkInstance();
                    }
                    MappingWindowInstance.WakeUp();
                    break;

                case RequestId.DeleteMappingElements:
                    MappingWindowInstance.DozeOff();
                    if (LinkedElementToDelete.Count > 0)
                    {
                        using (var trans = new Transaction(CurrentDocument))
                        {
                            trans.Start("Delete Element Maps");
                            try
                            {
                                foreach (var linkedInfo in LinkedElementToDelete)
                                {
                                    if (selectedLink.LinkedElements.ContainsKey(linkedInfo.LinkedElementId))
                                    {
                                        selectedLink.LinkedElements.Remove(linkedInfo.LinkedElementId);
                                        var linkedElement = CurrentDocument.GetElement(linkedInfo.LinkedElementId);
                                        if (null != linkedElement)
                                        {
                                            var removed = MoverDataStorageUtil.RemoveLinkedElementInfo(linkedElement);
                                        }
                                    }
                                }
                                trans.Commit();
                                if (LinkInstances.ContainsKey(selectedLink.InstanceId))
                                {
                                    LinkInstances.Remove(selectedLink.InstanceId);
                                    LinkInstances.Add(selectedLink.InstanceId, selectedLink);
                                }
                            }
                            catch (Exception ex)
                            {
                                trans.RollBack();
                                MessageBox.Show("Failed to delete element maps.\n" + ex.Message, "Delete Element Maps", MessageBoxButton.OK, MessageBoxImage.Warning);
                            }
                        }
                        MappingWindowInstance.RefreshLinkInstance();
                    }
                    MappingWindowInstance.WakeUp();
                    break;

                case RequestId.ShowElement:
                    MappingWindowInstance.DozeOff();
                    if (null != SelectedLinkedInfo)
                    {
                        if (null != CurrentDocument.GetElement(SelectedLinkedInfo.LinkedElementId))
                        {
                            HighlightElement();
                        }
                    }
                    MappingWindowInstance.WakeUp();
                    break;

                case RequestId.AddFamilyMapping:
                    FamilyWindowInstance.DozeOff();
                    if (null != SelectedFamilyInfo)
                    {
                        var tType = CurrentDocument.GetElement(SelectedFamilyInfo.TargetTypeId) as ElementType;
                        if (null != tType)
                        {
                            using (var trans = new Transaction(CurrentDocument))
                            {
                                trans.Start("Add Family Map");
                                try
                                {
                                    if (selectedLink.LinkedFamilies.ContainsKey(SelectedFamilyInfo.TargetTypeId))
                                    {
                                        selectedLink.LinkedFamilies.Remove(SelectedFamilyInfo.TargetTypeId);
                                    }
                                    selectedLink.LinkedFamilies.Add(SelectedFamilyInfo.TargetTypeId, SelectedFamilyInfo);

                                    var updated = MoverDataStorageUtil.UpdateLinkedFamilyInfo(SelectedFamilyInfo, tType);

                                    if (LinkInstances.ContainsKey(selectedLink.InstanceId))
                                    {
                                        LinkInstances.Remove(selectedLink.InstanceId);
                                        LinkInstances.Add(selectedLink.InstanceId, selectedLink);
                                    }
                                    trans.Commit();
                                    MappingWindowInstance.RefreshLinkInstance();
                                }
                                catch (Exception ex)
                                {
                                    trans.RollBack();
                                    var message = ex.Message;
                                }
                            }
                        }
                    }

                    FamilyWindowInstance.WakeUp();
                    break;

                case RequestId.DeleteFamilyMapping:
                    MappingWindowInstance.DozeOff();
                    if (null != SelectedFamilyInfo)
                    {
                        using (var trans = new Transaction(CurrentDocument))
                        {
                            trans.Start("Delete Family Map");
                            try
                            {
                                foreach (var familyInfo in LinkedFamilyToDelete)
                                {
                                    if (selectedLink.LinkedFamilies.ContainsKey(familyInfo.TargetTypeId))
                                    {
                                        selectedLink.LinkedFamilies.Remove(familyInfo.TargetTypeId);
                                        var tType = CurrentDocument.GetElement(familyInfo.TargetTypeId) as ElementType;
                                        if (null != tType)
                                        {
                                            var removed = MoverDataStorageUtil.RemoveLinkedFamilyInfo(tType);
                                        }
                                    }
                                }
                                trans.Commit();
                                if (LinkInstances.ContainsKey(selectedLink.InstanceId))
                                {
                                    LinkInstances.Remove(selectedLink.InstanceId);
                                    LinkInstances.Add(selectedLink.InstanceId, selectedLink);
                                }
                                MappingWindowInstance.RefreshLinkInstance();
                            }
                            catch (Exception ex)
                            {
                                trans.RollBack();
                                var message = ex.Message;
                            }
                        }
                    }
                    MappingWindowInstance.WakeUp();
                    break;

                case RequestId.None:
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to execute external event handler.\n" + ex.Message, "Execute External Event Handler", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }