Here is the code after some modification
public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet) { _cachedCmdData = cmdData; try { FilteredElementCollector column = new FilteredElementCollector(CachedDoc, CachedDoc.ActiveView.Id); column.OfClass(typeof(FamilyInstance)); column.OfCategory(BuiltInCategory.OST_StructuralColumns); foreach (FamilyInstance col in column) { FilteredElementCollector slab = new FilteredElementCollector(CachedDoc, CachedDoc.ActiveView.Id); slab.OfClass(typeof(FamilyInstance)); slab.OfCategory(BuiltInCategory.OST_StructuralFoundation); BoundingBoxXYZ bb = col.get_BoundingBox(CachedDoc.ActiveView); Outline outline = new Outline(bb.Min, bb.Max); BoundingBoxIntersectsFilter bbfilter = new BoundingBoxIntersectsFilter(outline); slab.WherePasses(bbfilter); foreach (FamilyInstance sal in slab) { using (Transaction t = new Transaction(CachedDoc, "EditType")) { t.Start(); string abc = col.Symbol.Name; string a = abc.Remove(0, 1); string familySymbolName = "F" + a; // Find all family symbols whose name is "F(x)" FilteredElementCollector collector = new FilteredElementCollector(CachedDoc); collector.OfCategory(BuiltInCategory.OST_StructuralFoundation); collector.OfClass(typeof(FamilySymbol)); // Get Element Id for family symbol which will be used to find family instances var query = from element in collector where (element.Name == familySymbolName) select element; // where element.Name == xyz; List<FamilySymbol> famSyms = query.Cast<FamilySymbol>().ToList<FamilySymbol>(); Element el = CachedDoc.GetElement(famSyms[0].Id); FamilySymbol symbol = famSyms[0] as FamilySymbol; symbol.Activate(); sal.Symbol = symbol as FamilySymbol; t.Commit(); } } } return Result.Succeeded; } catch (Exception ex) { msg = ex.ToString(); return Result.Failed; } }
Note: this code works only for Loadable Foundation Families. You will also will need to supress the following warning:
"An attached Structural Foundation will be moved to the bottom of the Column."
Then every thing will work as a charm.