Load as Group Revit API

Load as Group Revit API

bd5cents
Advocate Advocate
4,880 Views
9 Replies
Message 1 of 10

Load as Group Revit API

bd5cents
Advocate
Advocate

Is there a way to programmatically load a .rvt/.rvg through the utilization of a macro? We're trying to allow the user to select a group file and place that into the Revit project, but can't figure out what the right method would be. We've tried a similar approach to the Load Family option. It would be great help. Thank you!

4,881 Views
9 Replies
Replies (9)
Message 2 of 10

bd5cents
Advocate
Advocate

Here is the code I'm working with.

0 Likes
Message 3 of 10

bd5cents
Advocate
Advocate

public void LoadGroup()
        {    
            
            try{UIDocument uidoc = this.ActiveUIDocument;
                Document doc = uidoc.Document;
                string selectedDirectory = Environment.CurrentDirectory;
            
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.InitialDirectory = selectedDirectory;
                openFileDialog1.Filter = "Revit Families (*.rvt)|*.rvt";
                
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    using (Transaction tx1 = new Transaction(doc))
                    {
                    
                        tx1.Start("Start");
                        
                        Autodesk.Revit.DB.Family family = null;                        
                        Autodesk.Revit.DB.FamilySymbol familySymbol = null;
                        Autodesk.Revit.DB.Group group = null;
                        
                        if(doc.LoadFamilySymbol(openFileDialog1.FileName, "Groups"out familySymbol))
                        {
                        String name = group.Name;
                        TaskDialog.Show("Revit""The family has been loaded and it's name is " + name);
                        }
                        else if(doc.LoadFamily(openFileDialog1.FileName, out family))
                        {
                            String name2 = family.Name;
                            TaskDialog.Show("Revit""The group has been loaded and it's name is " + name2);
                        }
                        else
                        {
                        TaskDialog.Show("Revit""Cant load the family file.");
                        }    
                        
                        tx1.Commit();
                        
                    }
                
                }
                
            }
            
             catch(Autodesk.Revit.Exceptions.OperationCanceledException)
                {
                   
                }
            }
        }

0 Likes
Message 4 of 10

JimJia
Alumni
Alumni
Dear Brian Nickel, I am afraid the approach you have attempted is not correct. The LoadFamilySymbol or LoadFamily method can only load the .rfa file, it's not designed to deal with the .rvt file. If you want to load a .rvt file, you may need to use the RevitLinkType class, and if you still want to create a instance ,you may need to use the RevitLinkInstance class. Following is code example load the .rvt and create link instance: public void CreateLinkInst() { Document doc= this.Document; Transaction trans = new Transaction(doc,"Link"); trans.Start(); System.Windows.Forms.OpenFileDialog myDialog = new System.Windows.Forms.OpenFileDialog(); myDialog.Filter = "|*.rvt"; if (myDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string filePath = myDialog.FileName; ModelPath modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath); RevitLinkLoadResult result = RevitLinkType.Create(doc, modelPath, new RevitLinkOptions(true)); RevitLinkType elem = doc.GetElement(result.ElementId) as RevitLinkType; RevitLinkInstance.Create(doc, elem.Id); } trans.Commit(); }

Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes
Message 5 of 10

boostyourbim
Advocate
Advocate

Hi Jim,

 

Creating a link instance is not the same as loading a group. The desired result is a group, not a link. If it were possible to link the group RVT then bind the link that would get to the same end result.

 

But does the API provide the ability to bind an RVT link?

 

bind.JPG

0 Likes
Message 6 of 10

JimJia
Alumni
Alumni
Dear Brian Nickel,
Revit doesn't expose bind link API, we already create Revit issue REVIT-90538 to track this. We're sorry for this if it's bad news for you.

Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes
Message 7 of 10

JimJia
Alumni
Alumni
Revit also created [CF-987 API wish: Access "Load As Group" command under the Insert tab] to track load as group API.

Jim Jia
Autodesk Forge Evangelist
https://forge.autodesk.com
Developer Technical Services
Autodesk Developer Network
Email: Jim.Jia@autodesk.com
0 Likes
Message 8 of 10

Anonymous
Not applicable

Did you ever find a solution to this?  I am interested in a similar task; to invoke the same routine as the "insert as group" button.

0 Likes
Message 9 of 10

Anonymous
Not applicable

 I know its not exactly the same thing but I just made a workaround for this. If you load the group into a different project, you can open that project and copy/paste the group into your project using "ElementTransformUtils.CopyElements()" and just delete the placed group and the group type will stay in your model.

 

Edit:

You could also try (as a workaround) the postable command "Autodesk.Revit.UI.PostableCommand.LoadAsGroup" and then paste the path into the prompt  

Message 10 of 10

546737048
Community Visitor
Community Visitor

Hello,I am working on copying a group from one document to another after I read your advice. However, the code always fails. Would you please check my code and give some advice. Thanks a lot! Following is my code:

 

 

using System;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace HelloWorld
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)
{
TaskDialog.Show("Start", "Hello World!");

Application app = revit.Application.Application;
UIDocument uiDoc = revit.Application.ActiveUIDocument;
Document doc = uiDoc.Document;

using (Transaction trans = new Transaction(doc))
{
if (trans.Start("放置组1") == TransactionStatus.Started)
{
string address1 = @"C:\Users\machine\Desktop\组 1.rvt";
Document openDoc = app.OpenDocumentFile(address1);
FilteredElementCollector collector = new FilteredElementCollector(openDoc).OfClass(typeof(GroupType));
var query = from element in collector where element.Name == "组 1" select element;
List<Element> groups = query.ToList();
ElementId groupId = groups[0].Id;
ICollection<ElementId> copyIds = new Collection<ElementId>();
copyIds.Add(groupId);
ElementId copied = ElementTransformUtils.CopyElements(openDoc, copyIds, doc, Transform.Identity, new CopyPasteOptions()).First();

XYZ location = uiDoc.Selection.PickPoint("请指定插入房间的位置");
FilteredElementCollector collector1 = new FilteredElementCollector(doc).OfClass((typeof(GroupType)));
var groupTypes1 = from element in collector1 where element.Name == "组 1" select element;
GroupType groupType = groupTypes1.First() as GroupType;
Group groupCreated = doc.Create.PlaceGroup(location, groupType);

if (TransactionStatus.Committed == trans.Commit())
{
if (groupCreated != null)
{
TaskDialog.Show("创建成功", "组 1已包含在模型中");
}
else
{
TaskDialog.Show("创建失败", "没有创建组 1");
}
}
else
{
trans.RollBack();
}
}
}

return Result.Succeeded;
}
}
}

 

20180621152832.png

0 Likes