Set value of Parameter, EditFamily fails

Set value of Parameter, EditFamily fails

dirk.neethling
Advocate Advocate
1,917 Views
7 Replies
Message 1 of 8

Set value of Parameter, EditFamily fails

dirk.neethling
Advocate
Advocate

Hello,

 

whilst wanting to set the value of a Parameter, I get the following error on calling the appropriate family document:

 

            // Get Family document for family

            Document familyDoc = document.EditFamily(family);

 

 

exc.Message  "The document is currently modifiable! Close the transaction before calling EditFamily." String

 

I read Jeremy's post entitled:

 

http://thebuildingcoder.typepad.com/blog/2012/05/e​dit-family-requires-no-transaction.html

 

But what is the upshot? Do I need to close the transactions, then run the one specifically for EditFamily?

Which command?

 

Thanks!

 

Transactions were set to: [TransactionAttribute(TransactionMode.Manual)]

Here is the test code:

 

 

    public Result Execute(
     ExternalCommandData commandData,
     ref string message,
     ElementSet elements)
    {
        init();

        //Get application and document objects
        UIApplication uiApp = commandData.Application;
        Document doc = uiApp.ActiveUIDocument.Document;

        FilteredElementCollector coll = new FilteredElementCollector(doc);

        try
        {
            //Define a Reference object to accept the pick result.
            Reference pickedRef = null;

            //Pick a group
            Selection sel = uiApp.ActiveUIDocument.Selection;
            pickedRef = sel.PickObject(ObjectType.Element, "Please select a group");
            Element elem = doc.GetElement(pickedRef);

            Transaction trans = new Transaction(doc, "Set FamilyParameter");
            trans.Start();
            EditFamilyTypes_(doc, elem as FamilyInstance,
                "HT_STL_G_COMPONENT BORED TUNNEL TYPE",
                "HT_STL_G_ADAPTIVE BORED TUNNEL", 2.0);
            trans.Commit();

            MessageBox.Show("Execute completed");
        }
        catch (Exception exc)
        {
            Debug.WriteLine(exc);
        }

        return Result.Succeeded;
    }

 

 

    public void EditFamilyTypes_(Document document,
        FamilyInstance familyInstance, string parmname,
        string symbolName, double newval)
    {
        if (null != familyInstance.Symbol)
        {
            // Get family associated with this
            Family family = familyInstance.Symbol.Family;

            // Get Family document for family
            Document familyDoc = document.EditFamily(family);
            if (null != familyDoc)
            {
                FamilyManager familyManager = familyDoc.FamilyManager;

                // look for parmname parameter and set to newval m
                FamilyParameter familyParam = familyManager.get_Parameter(parmname);
                if (null != familyParam)
                {
                    familyManager.Set(familyParam, newval);
                }
            }
        }
    }

 

 

0 Likes
1,918 Views
7 Replies
Replies (7)
Message 2 of 8

jeremytammik
Autodesk
Autodesk

Dear Dirk,

 

Thank you for posting this question here in public.

 

You say "pity this is the last mail".

 

It is not a pity!

 

I am glad!

 

Now everybody can see this question, and I am one thousand times more motivated to answer it!

 

Don't you understand that?

 

As I explained to you, I do nothing at all that cannot be shared and published, and share and publish absolutely everything I work on.

 

Also, you apparently did not read the end of my last message to you.

 

I explicitly state:

 

In this specific case, just for you, here is a bit of research and a pointer to some existing discussions that might help answer the question:

 

http://thebuildingcoder.typepad.com/blog/2015/09/svg-in-memory-family-creation-and-revitlookup-bound...

http://thebuildingcoder.typepad.com/blog/2011/06/creating-and-inserting-an-extrusion-family.html

http://thebuildingcoder.typepad.com/blog/2012/05/edit-family-requires-no-transaction.html

http://thebuildingcoder.typepad.com/blog/2014/09/modifying-saving-and-reloading-families.html

 

The answer is right there.

 

All you have to do is read it.

 

It is not harder to read it in a public blog post than it would be to read it in a private email.

 

I promise!

 

Try it out, you'll see how easy it is!

 

Have fun, and good luck!

 

Thank you again for raising this issue!

 

Best regards

 

Jeremy



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

Message 3 of 8

jeremytammik
Autodesk
Autodesk

Actually, on rechecking those four posts, I see that some of them are not explicitly dealing with EditFamily.

 

Indeed, you do need to close the transaction.


So, you would be in manual mode, and then have something like this:

 

Manual mode

Execute

  using Transaction
    Start
    do stuff...
    Commit

  EditFamily

  using Transaction
    Start
    do more stuff...
    Commit

return Result.

 

Cheers,

 

Jeremy



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

Message 4 of 8

jeremytammik
Autodesk
Autodesk

Here is another answer to a query that looks quite similar to yours:

 

http://thebuildingcoder.typepad.com/blog/2011/06/reloading-a-family.html#comment-2402971511

 

Please let us know how you end up solving this.

 

Thank you!

 

Cheers,

 

Jeremy



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

Message 5 of 8

dirk.neethling
Advocate
Advocate

I will, I’m still busy coding.




Edited by
Discussion_Admin

0 Likes
Message 6 of 8

dirk.neethling
Advocate
Advocate

Hello Jeremy,

 

thanks for the pointer, I got it working. In order to simplify the test version I included everything in the Execute method.

The next step would be to open up the selected family from the rvt project in the Family Editor.

Dirk

 

 

    public Result Execute(
     ExternalCommandData commandData,
     ref string message,
     ElementSet elements)
    {
        init();

        //Get application and document objects
        UIApplication uiApp = commandData.Application;
        Document doc = uiApp.ActiveUIDocument.Document;
        Autodesk.Revit.ApplicationServices.Application app = uiApp.Application;

        FilteredElementCollector coll = new FilteredElementCollector(doc);

        try
        {
            //Define a Reference object to accept the pick result.
            Reference pickedRef = null;
            Family f2 = null;
            string fullpath = "";
            bool ret = false;
            IFamilyLoadOptions opt = new JtFamilyLoadOptions();

            var rootpaths = GetFilePaths(app, false);
            if (rootpaths.Count > 0)
                fullpath = Path.GetDirectoryName(rootpaths.Values.ElementAt(0));

            //Pick a group
            Transaction trans = new Transaction(doc, "Pick a group");
            trans.Start();
            Selection sel = uiApp.ActiveUIDocument.Selection;
            pickedRef = sel.PickObject(ObjectType.Element, "Please select a group");
            Element elem = doc.GetElement(pickedRef);
            trans.Commit();

            //string parmname = "HT_STL_G_COMPONENT BORED TUNNEL TYPE";
            String parmname1 = "HT_WIDTH_NOMINAL_COMPONENT";
            double newval1 = 2000;
            String parmname2 = "HT_INSIDE_RADIUS_COMPONENT";
            double newval2 = 3500;
            String parmname3 = "HT_THICKNESS_COMPONENT";
            double newval3 = 500;

            //if (null != familyInstance.Symbol)
            {
                Transaction trans2 = new Transaction(doc, "Pick a group");
                trans2.Start();
                FamilyInstance familyInstance = elem as FamilyInstance;

                // Get family associated with this
                Family family = familyInstance.Symbol.Family;
                trans2.Commit();

                // Get Family document for family
                Document familyDoc = doc.EditFamily(family);

                string pathToFamilyDoc = Path.Combine(fullpath, familyDoc.Title);

                TaskDialog.Show("Family PathName", pathToFamilyDoc);
                if (null != familyDoc)
                {
                    FamilyManager familyManager = familyDoc.FamilyManager;

                    Transaction trans3 = new Transaction(familyDoc, "Pick a group");
                    trans3.Start();
                    // look for parmname parameter and set to newval m
                    FamilyParameter familyParam = familyManager.get_Parameter(parmname1);
                    if (null != familyParam)
                        familyManager.SetValueString(familyParam, newval1.ToString());

                    familyParam = familyManager.get_Parameter(parmname2);
                    if (null != familyParam)
                        familyManager.SetValueString(familyParam, newval2.ToString());

                    familyParam = familyManager.get_Parameter(parmname3);
                    if (null != familyParam)
                        familyManager.SetValueString(familyParam, newval3.ToString());
                    trans3.Commit();

                    f2 = familyDoc.LoadFamily(doc, opt);
                    SaveAsOptions options = new SaveAsOptions();
                    options.OverwriteExistingFile = true;
                    options.SetWorksharingOptions(null);
                    familyDoc.SaveAs(pathToFamilyDoc, options);
                }
            }
            MessageBox.Show("Execute completed");
        }
        catch (Exception exc)
        {
            Debug.WriteLine(exc);
        }

        return Result.Succeeded;
    }

 

Message 7 of 8

jeremytammik
Autodesk
Autodesk

Dear Dirk,

 

Congratulations on getting it to work! Well done!

 

Opening the family in the editor should be easy enough using uiapp.OpenAndActivateDocument:

 

http://thebuildingcoder.typepad.com/blog/2015/03/automatically-open-a-project-on-startup.html

 

Oh, and you really need to encapsulate your transactions in using blocks:

 

http://thebuildingcoder.typepad.com/blog/2012/04/using-using-automagically-disposes-and-rolls-back.h...

 

Cheers,

 

Jeremy



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

Message 8 of 8

dirk.neethling
Advocate
Advocate
Yes thanks, I will clean up once the basic functionality is there.
0 Likes