Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

FaceArray to List<Face>

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
christian.stan
182 Views, 4 Replies

FaceArray to List<Face>

christian.stan
Advocate
Advocate

Hello, as you can see from my way of writing code, I'm really not very good in C#
Do I have to create a List<Face> to retrieve the FaceArray

public void placerUneBanche()
        {
        	
        	Document doc=this.Document;
        	UIDocument uidoc=Application.ActiveUIDocument;
        	
        	FilteredElementCollector collector = new FilteredElementCollector(doc);
			collector = collector.OfClass(typeof(FamilySymbol));
			// Get Element Id for family symbol which will be used to find family instances
			var query = from e in collector where e.Name == "Pn 240 x 280" select e;
			List<Element> famSyms = query.ToList<Element>();
			ElementId symbolId = famSyms[0].Id;
			FamilySymbol fs=doc.GetElement(symbolId) as FamilySymbol;
			
			Reference choixselection = uidoc.Selection.PickObject(ObjectType.Element);
			Element el=doc.GetElement(choixselection);
			Wall wal=el as Wall;
			
			Options options=new Options();
			options.ComputeReferences = true;
			GeometryElement geoelt=wal.get_Geometry(options);
			//Creating an array of faces
			FaceArray face;
			//Creating a list to retrieve faces from an array
			List<Face> lf= new List<Face>();
			//Get the geometry of the element
			foreach (GeometryObject gobj in geoelt)
			{
				Solid sol= gobj as Solid;
				if (sol!=null)
				{
					face=sol.Faces;
					foreach (Face f in face)
					{
						lf.Add(f);
					}
				}
			}
			//Creation of a query to filter by component of the normal vector
			var query2= from f in lf where f.ComputeNormal(UV.Zero).Normalize().Z==-1 select f;
        	List<Face> lff = query2.ToList<Face>();
        	//Sort the list by point Z
        	lff = lff.OrderBy(f => f.Evaluate(UV.Zero).Z).ToList();
        	//Found the face
        	Face facesupport=lff[0];
        	
        	//Found point on middle of the wall
        	Element elmur=doc.GetElement(choixselection);
			LocationCurve lcurve=elmur.Location as LocationCurve;
			XYZ ptsupport=lcurve.Curve.Evaluate(0.5,true);
			//Found the normal of the wall direction
			Transform t=lcurve.Curve.ComputeDerivatives(0.5,true);
            XYZ vecn=t.BasisX.Normalize();
  			//Create a Transaction for the new object
        	Transaction trans = new Transaction(doc, "placez une Instance");
            // Start the transaction
            trans.Start();
            FamilyInstance nvfam=doc.Create.NewFamilyInstance(facesupport,ptsupport,vecn,fs);
            trans.Commit();
        }

 

 

 

 

Do you have book references to progress in C# (preferably in French)

I thank you in advance.

Sincerely
christian.stan

0 Likes

FaceArray to List<Face>

Hello, as you can see from my way of writing code, I'm really not very good in C#
Do I have to create a List<Face> to retrieve the FaceArray

public void placerUneBanche()
        {
        	
        	Document doc=this.Document;
        	UIDocument uidoc=Application.ActiveUIDocument;
        	
        	FilteredElementCollector collector = new FilteredElementCollector(doc);
			collector = collector.OfClass(typeof(FamilySymbol));
			// Get Element Id for family symbol which will be used to find family instances
			var query = from e in collector where e.Name == "Pn 240 x 280" select e;
			List<Element> famSyms = query.ToList<Element>();
			ElementId symbolId = famSyms[0].Id;
			FamilySymbol fs=doc.GetElement(symbolId) as FamilySymbol;
			
			Reference choixselection = uidoc.Selection.PickObject(ObjectType.Element);
			Element el=doc.GetElement(choixselection);
			Wall wal=el as Wall;
			
			Options options=new Options();
			options.ComputeReferences = true;
			GeometryElement geoelt=wal.get_Geometry(options);
			//Creating an array of faces
			FaceArray face;
			//Creating a list to retrieve faces from an array
			List<Face> lf= new List<Face>();
			//Get the geometry of the element
			foreach (GeometryObject gobj in geoelt)
			{
				Solid sol= gobj as Solid;
				if (sol!=null)
				{
					face=sol.Faces;
					foreach (Face f in face)
					{
						lf.Add(f);
					}
				}
			}
			//Creation of a query to filter by component of the normal vector
			var query2= from f in lf where f.ComputeNormal(UV.Zero).Normalize().Z==-1 select f;
        	List<Face> lff = query2.ToList<Face>();
        	//Sort the list by point Z
        	lff = lff.OrderBy(f => f.Evaluate(UV.Zero).Z).ToList();
        	//Found the face
        	Face facesupport=lff[0];
        	
        	//Found point on middle of the wall
        	Element elmur=doc.GetElement(choixselection);
			LocationCurve lcurve=elmur.Location as LocationCurve;
			XYZ ptsupport=lcurve.Curve.Evaluate(0.5,true);
			//Found the normal of the wall direction
			Transform t=lcurve.Curve.ComputeDerivatives(0.5,true);
            XYZ vecn=t.BasisX.Normalize();
  			//Create a Transaction for the new object
        	Transaction trans = new Transaction(doc, "placez une Instance");
            // Start the transaction
            trans.Start();
            FamilyInstance nvfam=doc.Create.NewFamilyInstance(facesupport,ptsupport,vecn,fs);
            trans.Commit();
        }

 

 

 

 

Do you have book references to progress in C# (preferably in French)

I thank you in advance.

Sincerely
christian.stan

4 REPLIES 4
Message 2 of 5
ctm_mka
in reply to: christian.stan

ctm_mka
Advocate
Advocate

Technically no, you do not need a separate list, you should be able to accomplish what you want within the foreach loop on line 33 of your sample.

0 Likes

Technically no, you do not need a separate list, you should be able to accomplish what you want within the foreach loop on line 33 of your sample.

Message 3 of 5
christian.stan
in reply to: ctm_mka

christian.stan
Advocate
Advocate

hi, thank you for the answer, but I admit I don't know what to do with this one (this most certainly comes from my lack of skills in C#, I need to look very deeply into this point)

 

Sincerely

christian.stan

0 Likes

hi, thank you for the answer, but I admit I don't know what to do with this one (this most certainly comes from my lack of skills in C#, I need to look very deeply into this point)

 

Sincerely

christian.stan

Message 4 of 5
ctm_mka
in reply to: christian.stan

ctm_mka
Advocate
Advocate
Accepted solution

You don't necessarily need to do anything. is anything not working? are you getting any errors? or just looking to be more efficient with your coding?

You don't necessarily need to do anything. is anything not working? are you getting any errors? or just looking to be more efficient with your coding?

Message 5 of 5
christian.stan
in reply to: ctm_mka

christian.stan
Advocate
Advocate

Hi, indeed I'm looking to improve, but I don't want to waste your time, I have to work on C# (public private void everything still seems a little too nebulous to me)
I give you a solution
thank you for taking the time to answer me
sincerely
christian.stan

0 Likes

Hi, indeed I'm looking to improve, but I don't want to waste your time, I have to work on C# (public private void everything still seems a little too nebulous to me)
I give you a solution
thank you for taking the time to answer me
sincerely
christian.stan

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report