How to create a sweep in a family document and load it in a project document

How to create a sweep in a family document and load it in a project document

846081597
Enthusiast Enthusiast
815 Views
4 Replies
Message 1 of 5

How to create a sweep in a family document and load it in a project document

846081597
Enthusiast
Enthusiast

Hello,i want to create a sweep in a temporary family document and load the family document in a opened project document, i have two problems:

1、i want to create a new family document programmatically,first i need to find a template,but i always fail,the code is 

                                     ExternalCommandData commandData;

                                     Application app = commandData.Application.Application;

                                     string familyTemPath = System.IO.Path.Combine(app.FamilyTemplatePath, "公制结构框架 - 综合体和桁架");

the familyTemPath always shows  "\Templates\公制结构框架 - 综合体和桁架", it is not the correct family template path,it means “app.FamilyTemplatePath” doesn't work, i can not find the correct template path;

2、I can't retrieve paths, I can only write the path myself,but when i load the family document with a sweep,the project document has nothing,the code is 

                                     ExternalCommandData commandData;

                                     Application app = commandData.Application.Application;

                                     Document doc = commandData.Application.ActiveUIDocument.Document;

                                     string famTempl = @"C:\ProgramData\Autodesk\RVT 2016\Family Templates\Chinese\11公制结构                                       框架 - 综合体和桁架.rft";

                                     Document familyDoc = app.NewFamilyDocument(famTempl);//the document can be created                                               successfully

                                     Transaction transaction = new Transaction(familyDoc, "create a sweep"); 
                                     transaction.Start();

                                     Sweep wall = CreateSweep(familyDoc, sweepProfile);//This is a piece of encapsulated code for                                           creating sweep. I tested it can create a sweep successfully.

                                     Family family = familyDoc.LoadFamily(doc);

                                     transaction.Commit();

                                     familyDoc.Close(false);

There was no sweep in the project document after the code was run,and there was no mistake.i don't know where the problem is.

i need your help,thank you.

 

0 Likes
Accepted solutions (1)
816 Views
4 Replies
Replies (4)
Message 2 of 5

eason.kangEDLV4
Autodesk Support
Autodesk Support

Hi,

 

About the 1st question, there is a missing file extension at the end of your familyTemPath. Replace it with this line, then it should works as expected.

tring familyTemPath = System.IO.Path.Combine(app.FamilyTemplatePath, "公制结构框架 - 综合体和桁架.rft");

 

And the 2nd one,

 

This line of your code snippet will force Revit to discard all modification in the document, so please change it

 

From:

familyDoc.Close(false);

To:

familyDoc.Close();
//OR
familyDoc.Close(true);

 

Ref: http://www.revitapidocs.com/2018.1/5948b03d-5537-33d4-6e38-a8f16d5d6779.htm

 

Here is the content of my test project, and it works fine in Revit 2019.

 

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using System.IO;
using System;

namespace SFDC15009370
{
    [Transaction(TransactionMode.Manual)]
    public class CreateSweep : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var uiapp = commandData.Application;
            var app = uiapp.Application;
            var famTemplatePath = app.FamilyTemplatePath;
            var templateName = "公制结构框架 - 综合体和桁架";
            var filename = "SFDC15009370.rfa";
            var fileSavePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), filename);

            var templateFullName = Path.Combine(famTemplatePath, templateName + ".rft");

            Document famDoc = app.NewFamilyDocument(templateFullName);

            if(famDoc == null)
            {
                throw new Exception("Cannot open family document");
            }

            using (Transaction transaction = new Transaction(famDoc))
            {
                try
                {
                    transaction.Start("Create a sweep");

                    // Sketch plane is important and must match the curves below. 
                    // Here we use a sketch plane whose normal is X. 
                    // 
                    var geometryPlane = Plane.CreateByNormalAndOrigin(XYZ.BasisX, XYZ.Zero);
                    var sketchPlane = SketchPlane.Create(famDoc, geometryPlane);
                    var curveArr = new CurveArrArray();
                    var arr = new CurveArray();

                    // the units of the new arc angles are radians. 
                    // Creating a profile of a circle 
                    // 
                    arr.Append(Arc.Create(new XYZ(), 1.0, 0.0, 2 * Math.PI, XYZ.BasisX, XYZ.BasisY));
                    curveArr.Append(arr);
                    SweepProfile profile = famDoc.Application.Create.NewCurveLoopsProfile(curveArr);

                    // Two lines which proceed in the YZ plane defined by the sketch plane: OK. 
                    // If the second line were to go out of the sketch plane this would fail. 
                    var curves = new CurveArray();

                    var end0 = new XYZ(0, 0, 0); // start point of the arc 
                    double radius = 10.0;

                    var end1 = new XYZ(0, radius * (1.0 - Math.Sin(Math.PI / 6.0)), radius * Math.Cos(Math.PI / 6.0)); // end point of the arc 
                    var pointOnCurve = new XYZ(0, radius * (1.0 - Math.Sin(Math.PI / 3.0)), radius * Math.Cos(Math.PI / 3.0)); // point along arc 
                    var arc = Arc.Create(end0, end1, pointOnCurve);
                    curves.Append(arc);
                    Sweep sweep = famDoc.FamilyCreate.NewSweep(true, curves, sketchPlane, profile, 0, ProfilePlaneLocation.Start);
                    transaction.Commit();
                }
                catch(Exception ex)
                {
                    transaction.RollBack();
                    TaskDialog.Show("ADN", ex.Message);
                    return Result.Failed;
                }
            }

            famDoc.SaveAs(fileSavePath);
            famDoc.Close(true);

            //uiapp.OpenAndActivateDocument(fileSavePath);

            // Load family into current project
            var uidoc = uiapp.ActiveUIDocument;
            var doc = uidoc.Document;

            using (Transaction transaction = new Transaction(doc))
            {
                try
                {
                    transaction.Start("Load Sweep family");
                    Family family = null;
                    uidoc.Document.LoadFamily(fileSavePath, out family);
                    transaction.Commit();

                    if (family == null)
                    {
                        throw new Exception(string.Format("Failed to load {0}", fileSavePath));
                    }

                    TaskDialog.Show("ADN", string.Format("Family {0} loaded", family.Name));
                }
                catch (Exception ex)
                {
                    transaction.RollBack();
                    TaskDialog.Show("ADN", ex.Message);
                    return Result.Failed;
                }
            }

            return Result.Succeeded;
        }
    }
}

Hope it helps,

 

Cheers,

 

 


Eason Kang
Developer Advocate
Developer Advocacy & Support Service
Autodesk Platform Service (formerly Forge)

0 Likes
Message 3 of 5

846081597
Enthusiast
Enthusiast

Thankyou for your help,my second problem has solved with your method,thank you so much.

However,i still can not get the correct path when i copy your code,the path get in the program is 错误77777.png,i run it in Revit 2016

0 Likes
Message 4 of 5

eason.kangEDLV4
Autodesk Support
Autodesk Support
Accepted solution

Hi,

 

Please navigate to your Revit 2016 content install location (e.g. C:\ProgramData\Autodesk\RVT 2016\Family Templates\Chinese) with your file explorer and check if there is a template named with "公制结构框架 - 综合体和桁架.rft" inside. My "公制结构框架 - 综合体和桁架.rft" is located in "C:\\ProgramData\\Autodesk\\RVT 2016\\Family Templates\\Chinese\\公制结构框架 - 综合体和桁架.rft"

 

If it is there, I would advise you to use Directory.GetFiles Method to fetch all filenames in your template path, then do string comparisons to see whether there is an issue on the template filename. For instance,

string templateFullName;

string [] fileEntries = Directory.GetFiles(app.FamilyTemplatePath);
foreach(string filename in fileEntries)
{
	if(filename.Contains("公制结构框架 - 综合体和桁架"))
	{
		templateFullName = filename;
		break;
	}
}

Document famDoc = app.NewFamilyDocument(templateFullName);

 

Meanwhile, please make sure your cs files are encoded in UTF-8. I'm able to access the "公制结构框架 - 综合体和桁架.rft" of Revit 2016 in my computer without any problem.

 

encoding.jpg

Hope it helps.

 

Cheers,

 

 

 


Eason Kang
Developer Advocate
Developer Advocacy & Support Service
Autodesk Platform Service (formerly Forge)

Message 5 of 5

846081597
Enthusiast
Enthusiast

Thank you for your help. You are so kind.

I  run "app.FamilyTemplatePath"  in my colleagues'computer successfully ,i can get the correct template path ,but it failed on my computer,maybe it was because I installed the Revit on the D-disk when I first installed it.

Thank you very much.

0 Likes