Add Revit Links to Project with C#

Anonymous

Add Revit Links to Project with C#

Anonymous
Not applicable

Hi all,

 

I'm in my first attempt to create some custom Add-ins for Revit written in C# and the first tool I'm struggling to get working is one that takes some *.rvt files from a folder and links them in the active project.

 

In order to get started I simplified the functionalities of the script and right now I'm just trying to link a file that is hard coded in the script but it is still not working and I'm not too sure on the reason. I suspect it might have to do with the transaction needing to be started and/or closed.

 

This is where I got so far with my code:

 

#region Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Collections;
using System.Linq;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.Creation;
#endregion

namespace LinkRVT_test
{
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            // Get application and document objects

            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
            Autodesk.Revit.DB.Document doc = uidoc.Document;

            try
            {
                using (Transaction transaction = new Transaction(doc))
                {
                    transaction.Start("Link files");

                    string testFile = @"C: \Users\atassera\AT\C#\Revit\LinkRVT_test\Utilities\AR-GAM-3DM-FACADE-RVT.rvt";

                    ModelPath linkpath = ModelPathUtils.ConvertUserVisiblePathToModelPath(testFile);

                    RevitLinkOptions options = new RevitLinkOptions(false);

                    RevitLinkLoadResult result = RevitLinkType.Create(doc, linkpath, options);

                    RevitLinkInstance.Create(doc, result.ElementId);
                    RevitLinkInstance instance2 = RevitLinkInstance.Create(doc, result.ElementId);
                    Location location = instance2.Location;
                    return Result.Succeeded;

                }
            }

When I start debugging, Revit opens and I go in the Add-Ins panel, External Tools and click the tool I'm creating but nothing happens.

 

Any help is greatly appreciated!

 

Thank you

 

Andrea

0 Likes
Reply
Accepted solutions (2)
6,264 Views
25 Replies
Replies (25)

Dale.Bartlett
Collaborator
Collaborator

1. Yes, the .ShowDialog() does what is says
2. The openFileDialog1 variable is an OpenFileDialog object, one property of which is .FileNames
3. There was nothing wrong with your array variable: string[] filesInFolder = openFileDialog1.FileNames;

 

I have had so much help on these forums, the odd time I can return that is a real pleasure. Dale 




______________
Yes, I'm Satoshi.
0 Likes

Anonymous
Not applicable

Hey guys,

 

I was wondering if there's a way to specify the positioning of the links within the script. Similarly to what you do in

Positioning.PNG

 

I had a look in the APIs to the properties and to the options but I didn't really find anything.

 

Am I missing something?

 

Thanks!

 

Andrea

0 Likes

jeremytammik
Autodesk
Autodesk

Dear Andrea,

 

I asked the development team for you.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Anonymous
Not applicable

Hi @jeremytammik,

 

Thank you very much for the help! Much appreciated!

 

Let me know if you have any news.

 

Thanks,

 

Andrea

0 Likes

jeremytammik
Autodesk
Autodesk

Dear Andrea,

 

The development team replied:

 

You can do origin-to-origin or by-shared-coordinates. If you want another placement option you have to move the instance after the fact. This means there's no elegant way to do center-to-center. We think most users don't care about that option; please let us know if you think differently.

 

It's an option on RevitLinkInstance.Create(), not RevitLinkType. There's a new version of the instance creation function which takes an ImportPlacement and lets you pick By Shared Coordinates. This is available in Revit 2018 and later versions.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes

Anonymous
Not applicable

Thanks @jeremytammik,

 

That's exactly what I needed and origin-to-origin or by-shared-coordinates are actually what I care about. I went checking the options though and I found this:

PositioningAPI.PNG

Now, I guess Origin and Shared stand for origin-to-origin and by-shared-coordinates. So what are Site and Centered?

 

Thanks!

 

Andrea

0 Likes