Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Add Revit Links to Project with C#

25 REPLIES 25
SOLVED
Reply
Message 1 of 26
Anonymous
5533 Views, 25 Replies

Add Revit Links to Project with C#

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

25 REPLIES 25
Message 21 of 26
Dale.Bartlett
in reply to: Anonymous

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.
Message 22 of 26
Anonymous
in reply to: Dale.Bartlett

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

Message 23 of 26
jeremytammik
in reply to: Anonymous

Dear Andrea,

 

I asked the development team for you.

 

Cheers,

 

Jeremy



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

Message 24 of 26
Anonymous
in reply to: jeremytammik

Hi @jeremytammik,

 

Thank you very much for the help! Much appreciated!

 

Let me know if you have any news.

 

Thanks,

 

Andrea

Message 25 of 26
jeremytammik
in reply to: Anonymous

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

Message 26 of 26
Anonymous
in reply to: jeremytammik

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Rail Community