Message 1 of 5
NewFamilyInstance difference between 2018 and 2019
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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 :
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 :
Result with Revit 2019 :
I attach a folder with Revit files and code. Can someone help me !!
Thanks ! ![]()