Load family from website

Load family from website

vanlion
Advocate Advocate
2,657 Views
4 Replies
Message 1 of 5

Load family from website

vanlion
Advocate
Advocate

Hello,

 

Our company had made a tool for our client that loads families from

a local location on the computer. 

 

I'd like to know if it's also possible to load a family from a website?

We want to make sure the latest family is used by the users

 

Is searched it on the forum but couldn't find anything.

 

Anyone have some ideas?

 

Or is is more a c# question en use something like this example:

 

string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;

// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
    myStringWebResource = remoteUri + fileName;
    // Download the Web resource and save it into the current filesystem folder.
    myWebClient.DownloadFile(myStringWebResource, fileName);        
}
0 Likes
Accepted solutions (1)
2,658 Views
4 Replies
Replies (4)
Message 2 of 5

vanlion
Advocate
Advocate

So in my opinion the only difference in my code will be a replacement of the @"D:\testfamilie.rfa" with an url from a

website? or is that to easy.

 

Second question in this existing code, we using it with a pushbutton. When you load again you get an error. But is there a way to add it again without an error? So for example in Revit, when you press the Component button it's also loads again a component that is already been loaded without the error?

 

Thanks en hopefully someone can help with both questions 🙂

 

using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Structure;

namespace Loadf
{
    [Transaction(TransactionMode.Manual)]
    public class Loadfamily : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;

            String fileName = @"D:\testfamilie.rfa";

            using (Transaction t = new Transaction(doc, "family"))

                try
                {
                    t.Start("param");
                    //try to load family
                    Family family = null;
                    if (!doc.LoadFamily(fileName, out family))
                    {
                        throw new Exception("Unable to load " + fileName);
                    }

                    //loop trough door symbols and add a new table
                    ISet<ElementId> familySymbolIds = family.GetFamilySymbolIds();
                    double x = 0.0, y = 0.0;
                    foreach (ElementId id in familySymbolIds)
                    {
                        FamilySymbol symbol = family.Document.GetElement(id) as FamilySymbol;
                        XYZ location = new XYZ(x, y, 0);

                        if (!symbol.IsActive)
                        { symbol.Activate(); doc.Regenerate(); }

                        FamilyInstance instance = doc.Create.NewFamilyInstance(location, symbol, StructuralType.NonStructural);

                    }
                    t.Commit();
                    return Result.Succeeded;
                }
                catch (Exception ex)
                {
                // If something went wrong return Result.Failed
                message = ex.Message;
                return Result.Failed;
            }        
        }
    }     
}
0 Likes
Message 3 of 5

jeremytammik
Autodesk
Autodesk
Accepted solution

First question: yes, two steps: download file to local, and then load local file.

 

Second question: yes; two solutions:

 

Dirty solution: simply catch the exception thrown by the reload, ignore it, and continue.

 

Much cleaner: check whether the family has already been loaded, and only load it is really needed.

 

The clean solution is demonstrated by several samples by The Building Coder, e.g.,:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.25.3

  

Cheers,

 

Jeremy

 



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

Message 4 of 5

vanlion
Advocate
Advocate

Thanks for the useful answers. Going to save it as local and open from there.

 

We also want to add the option to change parameters before placing the family in the project

i tried the part i found on the topic from the link below. But that isn't working and second can you also do this with

get_Parameter(BuiltInParameter.ALL_MODEL_COST) for example?

 

https://forums.autodesk.com/t5/revit-api-forum/set-family-parameter-in-instance/m-p/7518933/highligh...

 

namespace Loadf
{
    [Transaction(TransactionMode.Manual)]
    public class Loadfamily : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;

            String fileName = @"D:\testfamilie.rfa";

            using (Transaction t = new Transaction(doc, "family"))

                try
                {
                    t.Start("param");
                    //try to load family
                    Family family = null;
                    if (!doc.LoadFamily(fileName, out family))
                    {
                        throw new Exception("Unable to load " + fileName);
                    }

                    //loop trough door symbols and add a new table
                    ISet<ElementId> familySymbolIds = family.GetFamilySymbolIds();
                    double x = 0.0, y = 0.0;
                    foreach (ElementId id in familySymbolIds)
                    {
                        FamilySymbol symbol = family.Document.GetElement(id) as FamilySymbol;
                        XYZ location = new XYZ(x, y, 0);

                        if (!symbol.IsActive)
                        { symbol.Activate(); doc.Regenerate(); }

                        FamilyInstance instance = doc.Create.NewFamilyInstance(location, symbol, StructuralType.NonStructural);
                 
                            foreach (Parameter p in instance.Parameters)

                            {
                                if (p.Definition.Name == "Width")

                                    p.Set(500);
                            }          
                    }
                    t.Commit();
                    return Result.Succeeded;
                }
                catch (Exception ex)
                {
                // If something went wrong return Result.Failed
                message = ex.Message;
                return Result.Failed;
            }        
        }
    }     
}
0 Likes
Message 5 of 5

jesseshipp8268
Observer
Observer

Dirty solution: simply catch the exception local painter and decorator thrown by the reload, ignore it, and continue.

0 Likes