NewFamilyInstance difference between 2018 and 2019

NewFamilyInstance difference between 2018 and 2019

Charles.Piro
Advisor Advisor
903 Views
4 Replies
Message 1 of 5

NewFamilyInstance difference between 2018 and 2019

Charles.Piro
Advisor
Advisor

Hi,

 

I created a code for insert family along a topography and following a line. My code is fully fonctional in Revit 2018 but not in 2019. I have not seen update in API for this function : 

 

Revit APIDoc.png

 

 

 

 

There is my code : 

 

 

[Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        private static Document doc;

        public Result Execute(ExternalCommandData extCmdData, ref string msg, ElementSet elmtSet)
        {
            UIApplication uiapp = extCmdData.Application;
            UIDocument uiDoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            doc = uiDoc.Document;

            //Must be in 3DView :
            if(!(doc.ActiveView is View3D))
            {
                TaskDialog.Show("debug", "Must be in 3DView");
                return Result.Failed;
            }

            string strFamilyName = "Test";
            string strPathFamily = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            double dblLengthFamily = UnitUtils.ConvertToInternalUnits(1025,DisplayUnitType.DUT_MILLIMETERS);//millimeters

            try
            {
                //Find or Load Family
                Family m_Family = null;
                FamilySymbol m_Sym = null;

                FilteredElementCollector fltElmntCol = new FilteredElementCollector(doc).OfClass(typeof(Family));
                int n = fltElmntCol.Count<Element>(e => e.Name.Equals(strFamilyName));

                if (n == 0)
                {
                    string strFamilyPath = Path.Combine(strPathFamily, strFamilyName + ".rfa");
                    using (Transaction trans = new Transaction(doc))
                    {
                        trans.Start("Load Family");
                        doc.LoadFamily(strFamilyPath, out m_Family);
                        trans.Commit();
                    }
                }
                else
                    m_Family = fltElmntCol.First<Element>(e => e.Name.Equals(strFamilyName)) as Family;

                if(m_Family == null)
                {
                    TaskDialog.Show("debug", "error family Load");
                    return Result.Failed;
                }

                using (Transaction trans = new Transaction(doc))
                {
                    trans.Start("Activate Family");
                    foreach (ElementId elmntId in m_Family.GetFamilySymbolIds())
                    {
                        m_Sym = doc.GetElement(elmntId) as FamilySymbol;
                        m_Sym.Activate();
                        break;
                    }
                    trans.Commit();
                }

                //Select Model Line in View
                Reference refsel = uiDoc.Selection.PickObject(ObjectType.Element, "Select Model Line");
                Element elmnt = doc.GetElement(refsel.ElementId);

                if(!(elmnt is ModelLine))
                {
                    TaskDialog.Show("debug", "Error select element");
                    return Result.Failed;
                }

                ModelLine m_Line = elmnt as ModelLine;
                Curve m_Curve = m_Line.GeometryCurve;
                double dblTotalLenth = m_Line.GeometryCurve.Length;
                XYZ ptStart = m_Curve.GetEndPoint(0);
                XYZ ptEnd = m_Curve.GetEndPoint(1);

                //
                int iNum = (int)(dblTotalLenth / dblLengthFamily);
                XYZ ptTemp = ptStart;
                List<XYZ> lstTemp = new List<XYZ>();
                List<XYZ> lstProj = new List<XYZ>();
                lstTemp.Add(ptTemp);
                lstProj.Add(ProjectPt(ptTemp));

                //create points for insert Family
                for (int i = 0; i < iNum; i++)
                {
                    Arc newArc = Arc.Create(ptTemp, dblLengthFamily, 0, 2 * Math.PI, XYZ.BasisX, XYZ.BasisY);

                    IntersectionResultArray inter = new IntersectionResultArray();
                    IntersectionResultArray interTemp;
                    newArc.Intersect(m_Curve, out interTemp);
                    IntersectionResultArrayIterator enumArray = interTemp.ForwardIterator();
                    while (enumArray.MoveNext())
                    {
                        inter.Append(enumArray.Current as IntersectionResult);
                    }

                    List<XYZ> lstPnts = new List<XYZ>();
                    foreach (IntersectionResult intersect in inter)
                    {
                        if (!Contains(lstTemp, intersect.XYZPoint))
                            lstPnts.Add(intersect.XYZPoint);
                    }
                    if (lstPnts.Count == 0)
                        continue;
                    else if (lstPnts.Count == 1)
                        ptTemp = lstPnts.First();
                    else
                    {
                        double d1, dmin = double.MaxValue;
                        foreach (XYZ pt in lstPnts)
                        {
                            d1 = pt.DistanceTo(ptEnd);
                            if (d1 < dmin)
                            {
                                dmin = d1;
                                ptTemp = pt;
                            }
                        }
                    }
                    lstTemp.Add(ptTemp);
                    lstProj.Add(ProjectPt(ptTemp));
                }

                //Insert Families
                using (Transaction trans = new Transaction(doc))
                {
                    trans.Start("Insert Family");

                    for (int i = 0; i < lstProj.Count - 1; i++)
                    {
                        try
                        {
                            XYZ vectOrient = lstProj[i + 1] - lstProj[i];
                            FamilyInstance faminst = doc.Create.NewFamilyInstance(lstProj[i], m_Sym, vectOrient, null, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                            doc.Regenerate();
                        }
                        catch (Exception e)
                        {
                            continue;
                        }
                    }
                    trans.Commit();
                }
            }
            catch (Exception e)
            {
                msg = e.Message;
                return Result.Failed;
            }
            return Result.Succeeded;
        }

        internal static XYZ ProjectPt(XYZ pnt)
        {
            ReferenceIntersector intersector = null;
            XYZ ptProject = null;
            try
            {
                intersector = new ReferenceIntersector(new ElementCategoryFilter(BuiltInCategory.OST_Topography), FindReferenceTarget.All, doc.ActiveView as View3D);
                intersector.FindReferencesInRevitLinks = true;

                ReferenceWithContext rwC = null;
                rwC = intersector.FindNearest(pnt, XYZ.BasisZ.Negate());
                if (rwC == null)
                    rwC = intersector.FindNearest(pnt.Add(new XYZ(0, 0, (108260 - pnt.Z))), XYZ.BasisZ.Negate());

                Reference refselrwc = rwC.GetReference();
                ptProject = refselrwc.GlobalPoint;
            }
            catch (Exception)
            {

            }

            return ptProject;
        }

        internal bool Contains(List<XYZ> lst, XYZ pnt)
        {
            List<XYZ> lstpnt = (from xyz in lst
                                where xyz.IsAlmostEqualTo(pnt)
                                select xyz).ToList();
            if (lstpnt != null && lstpnt.Count > 0)
                return true;
            else
                return false;
        }
    }

 

 

Result with Revit 2018 :

Good FamilyInstance.png

Result with Revit 2019 :

Error FamilyInstance.png

 

I attach a folder with Revit files and code. Can someone help me !!

 

Thanks ! Smiley Wink

 

 

 

 

 

 

 

 

 

 

 



PIRO Charles
Developer

PIRO CIE
Linkedin


0 Likes
904 Views
4 Replies
Replies (4)
Message 2 of 5

jeremytammik
Autodesk
Autodesk

Dear Charles,

 

Thank you for your query.

 

Your image of the 'Good FamilyInstance' surprises me.

 

It seems to imply that the family instances that you place change shape depending on the model line that they were placed on. They seem to be adapting from a right-angle form to a skewed one.

 

I am not aware of any possibility to achieve such behaviour at all without making use of adaptive components.

 

You say that the API behaves differently in Revit 2018 and 2019.

 

How about the user interface?

 

Can you successfully achieve the desired behaviour manually through the user interface in both versions?

 

Or do you observe a difference in that case as well?

 

Best regards,

 

Jeremy

 



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

0 Likes
Message 3 of 5

Revitalizer
Advisor
Advisor

Hi,

 

long while ago, I've made a similar "fence" tool.

 

It used a Family which could be skewed by setting parameters.

@jeremytammikNo AdaptiveComponent needed, at all.

 

May it be that you are using a similar approach?

 

I case of that:

If you have migrated your Family to Revit 2019, did you test it after that?

Are the parameters set correctly, after placement?

Does the content perform in the way you expect?

 

Revitalizer

 




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 4 of 5

charles
Contributor
Contributor

... account error

0 Likes
Message 5 of 5

Charles.Piro
Advisor
Advisor

Hi Jeremy,

 

Thank you for your answer.

 

When I place my families, the shape doesn't change. It's always the original shape.

In Revit 2018, families follow line direction in 3 axis : X,Y,Z but in Revit 2019, families follow line only in 2 directions : X and Y.

 

In my last post, I attached the sample code for revit 2018 and 2019.

I tried to place families manually and all was fine for 2018 and 2019.

 

Revitalizer : Thanks too !

My family is ok in 2019 and I don't use parameters. Only follow line direction for placement.



PIRO Charles
Developer

PIRO CIE
Linkedin


0 Likes