Community
place a face base family at wall or beam is OK, but at column is wrong.
My location xyz is right, but...
private static void CreateInstancesOnHostFamilyFaces(FamilySymbol familySymbol, FamilyInstance hostInstance)
{
var faces = GetSolids(hostInstance)
.SelectMany(x => x.Faces.OfType<PlanarFace>());
var doc = familySymbol.Document;
using (var transaction = new Transaction(doc, "create families"))
{
transaction.Start();
if (!familySymbol.IsActive)
familySymbol.Activate();
foreach (var planarFace in faces)
{
var faceBox = planarFace.GetBoundingBox();
var center = planarFace.Evaluate(0.5 * (faceBox.Max + faceBox.Min));
doc.Create.NewFamilyInstance(planarFace, center, XYZ.Zero, familySymbol);
}
transaction.Commit();
}
}
private static IEnumerable<Solid> GetSolids(Element element)
{
var geometry = element
.get_Geometry(new Options { ComputeReferences = true });
if (geometry == null)
return Enumerable.Empty<Solid>();
return GetSolids(geometry)
.Where(x => x.Volume > 0);
}
private static IEnumerable<Solid> GetSolids(IEnumerable<GeometryObject> geometryElement)
{
foreach (var geometry in geometryElement)
{
var solid = geometry as Solid;
if (solid != null)
yield return solid;
var instance = geometry as GeometryInstance;
if (instance != null)
foreach (var instanceSolid in GetSolids(instance.GetInstanceGeometry()))
yield return instanceSolid;
var element = geometry as GeometryElement;
if (element != null)
foreach (var elementSolid in GetSolids(element))
yield return elementSolid;
}
}
public Autodesk.Revit.UI.Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet eleSet)
{
Selection selection = cmdData.Application.ActiveUIDocument.Selection;
m_doc = cmdData.Application.ActiveUIDocument.Document;
FamilyInstance ins= null;
try
{
Reference selected = selection.PickObject(ObjectType.Element, "select:");
Element elemPick = m_doc.GetElement(selected.ElementId);
ins = elemPick as FamilyInstance;
}
catch (System.Exception)
{
return Result.Succeeded;
}
FamilySymbol type = null;
FilteredElementCollector elemCollector = new FilteredElementCollector(doc);
elemCollector.OfClass(typeof(FamilySymbol));
foreach (Element ent in elemCollector)
{
FamilySymbol obj = ent as FamilySymbol;
if (obj.Family.Name.CompareTo("famName") == 0)
{
type = obj;
break;
}
}
CreateInstancesOnHostFamilyFaces(type, ins);
return Result.Succeeded;
}
Hi @Anonymous . You need to use NewFamilyInstance method with reference direction. You can find the example here.
Best regards, Pavel Plotitsyn.
thank u, but that sample can not help me.
My code at beam and wall is OK! So, location XYZ is right.
But ONLY at column is wrong.
I think column is special.
@Anonymous ,
Hi,
Were you able to find a solution to the problem?
Regards,
Can't find what you're looking for? Ask the community or share your knowledge.