C# zero touch node issue

Hello there
could you please give an advice about what’s goining wrong in below code to create structural column vertical in revit through C# ?

public static List<Autodesk.Revit.DB.FamilyInstance> TestCol(List<Autodesk.DesignScript.Geometry.Curve> Column_axes, List<Revit.Elements.FamilyType> Family_Type)
{

        List<Autodesk.Revit.DB.FamilyInstance> cv = new List<Autodesk.Revit.DB.FamilyInstance>();
        Autodesk.Revit.DB.Structure.StructuralType st = Autodesk.Revit.DB.Structure.StructuralType.Column;
        TransactionManager.Instance.EnsureInTransaction(DocumentManager.Instance.CurrentDBDocument);

        foreach (Autodesk.DesignScript.Geometry.Curve C in Column_axes)
        {
            foreach (var type in Family_Type)
            {
                var cc = (DocumentManager.Instance.CurrentDBDocument.Create.NewFamilyInstance(C.ToRevitType(), type.InternalElement as FamilySymbol, DocumentManager.Instance.CurrentDBDocument.ActiveView.GenLevel, st));
                cv.Add(cc);
            }
        }
        TransactionManager.Instance.TransactionTaskDone();
        return cv;


}

FamilySymbol’s have to be activated before you can place them. Welcome to the wonderful world of the Revit API. Activation happens automatically if you have already placed an instance of that type in your document (manually). However as a dev you can activate using:

var mySymbol = type.InternalElement as FamilySymbol;

mySymbol.Activate();

Activate() also has to be called inside a transaction.

One other tip, don’t do this as its bad programming:

var cc = (DocumentManager.Instance.CurrentDBDocument.Create.NewFamilyInstance(C.ToRevitType(), type.InternalElement as FamilySymbol, DocumentManager.Instance.CurrentDBDocument.ActiveView.GenLevel, st));
                cv.Add(cc);

Instead, be more expressive as it improves code readability and avoids unnecessary repeated calls to the same object:

var familyInstances = new List<FamilyInstance>();

var doc = DocumentManager.Instance;

var docFactory = doc.CurrentDBDocument.Create;

var revitCurve = C.ToRevitType(); // C is also bad; use camelCase and dont abbreviate. 

var hostLevel = doc.ActiveView.GenLevel;

var familySymbol =  type.InternalElement as FamilySymbol;

var familyInstance = docFactory.NewFamilyInstance(revitCurve, familySymbol, hostLevel);

familyInstances.Add(familyInstance);
1 Like

Perfect Mr.Mahon … Both solution were tested and both are working like a charm :slight_smile: ,
but really I will prefer second one to active symbol while coding as a dev beginner :).

Thank you so much for your cooperation I appreciate it indeed Mr.BimOrph :slight_smile: