Add point to floor using C#

Add point to floor using C#

ahmadkhalaf7892
Advocate Advocate
1,452 Views
11 Replies
Message 1 of 12

Add point to floor using C#

ahmadkhalaf7892
Advocate
Advocate

TOp View.PNG

North view.PNG

 Hi Guys Is there any way I can add point to the floor ? 
1- I want to choose specific end points of the floor and add a point to them
2-moreover how can I make the floor have a variable thickness ? 

0 Likes
Accepted solutions (1)
1,453 Views
11 Replies
Replies (11)
Message 2 of 12

caroline.gitonga
Autodesk
Autodesk

Hi @ahmadkhalaf7892,

1. You can use the SlabShapeEditor class to get the floor slab object. You can use the SlabShapeEditor.DrawPoint method to add a point to the corresponding floor.

 

2.For to modify the floor tickness you will need to get the FloorType, then get the type's "GetCompoundStructure()" and from that, GetLayers(), then modify the layer you want to change the thickness of. (note that "width" is thickness when we're talking about floors). You may check this thread for more info on floor thickness

https://forums.autodesk.com/t5/revit-api-forum/how-to-modify-floor-thickness/td-p/8107091

Carol Gitonga, Developer Advocacy and Support, ADN Open
0 Likes
Message 3 of 12

ahmadkhalaf7892
Advocate
Advocate
Hi Caroline, Thanks you . I have read before about the slabshapeeditor.drawpoint however I didn't know how Can I use it because I have a little knowledge in C# language , Can you please just give me any simple example of How can I use this method ? it would help me a lot
0 Likes
Message 4 of 12

caroline.gitonga
Autodesk
Autodesk

Hi @ahmadkhalaf7892 ,

Here is the simple code I have used to add points to the floor slab:

public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
//Define a reference Object to accept the pick result
Reference pickedref = null;

//Pick floor
Selection sel = uiapp.ActiveUIDocument.Selection;
pickedref = sel.PickObject(ObjectType.Element, "Please select floor");
Element elem = doc.GetElement(pickedref);
Floor floor = elem as Floor;


//get the floor element

//declare slabshapeeditor
SlabShapeEditor slabshapeedit = floor.SlabShapeEditor;
//start a transaction

using (Transaction t = new Transaction(
doc, "Add Point To Floor"))
{
t.Start();

slabshapeedit.DrawPoint(new XYZ(29.4603524229075, 14.6861233480176, 0));


t.Commit();
}
TaskDialog.Show("Revit", "Point Added");

return Result.Succeeded;
// throw new NotImplementedException();
}
}

Important: If the input location is not on the top face of the slab, this function will return a null reference ( Nothing in Visual Basic) . Drawing a point on boundary crease may increase the number of creases. This method will regenerate the document even in manual regeneration mode.

You may try implement on your end matching what you mean to achieve and let me know.

Carol Gitonga, Developer Advocacy and Support, ADN Open
Message 5 of 12

caroline.gitonga
Autodesk
Autodesk
Hi, did you try the above sample? You may provide feedback for us to know if you need more help.
Carol Gitonga, Developer Advocacy and Support, ADN Open
0 Likes
Message 6 of 12

ahmadkhalaf7892
Advocate
Advocate

Hi Caroline , I am using this code below , however I am getting an error , Do you know what I am doing wrong ? the floor is being created normally when there is no slabshapeditor Method, however when I use it I get this error. .

Problem.PNG

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using System.Reflection.Emit;
using Autodesk.Revit.DB.Visual;


namespace FloorTest
{
[Transaction(TransactionMode.Manual)]

public class FloorTest : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{

// Get Document and UIDocument
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Autodesk.Revit.DB.Document doc = commandData.Application.ActiveUIDocument.Document;
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(FloorType));
FloorType floorType = collector.FirstElement() as FloorType;

// Build a floor profile for the floor creation
XYZ first = new XYZ(0, 0, 0);
XYZ second = new XYZ(20, 0, 0);
XYZ third = new XYZ(20, 15, 0);
XYZ fourth = new XYZ(0, 15, 0);

CurveLoop profile = new CurveLoop();
profile.Append(Line.CreateBound(first, second));
profile.Append(Line.CreateBound(second, third));
profile.Append(Line.CreateBound(third, fourth));
profile.Append(Line.CreateBound(fourth, first));

 

FilteredElementCollector levCollector = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).WhereElementIsNotElementType();
Level lev = levCollector.FirstElement() as Level;

Transaction actrans = new Transaction(doc);
actrans.Start("create Sloped Floor");
Floor Floor= Floor.Create(doc, new List<CurveLoop> { profile }, floorType.Id, lev.Id);


//declare slabshapeeditor
SlabShapeEditor slabshapeedit = Floor.SlabShapeEditor;
//start a transaction

 

slabshapeedit.DrawPoint(new XYZ(0, 15, 15));

 

actrans.Commit();
return Result.Succeeded;

}

}
}

0 Likes
Message 7 of 12

caroline.gitonga
Autodesk
Autodesk

Hi @ahmadkhalaf7892 ,

From the Revit API documentation of the DrawPoint method it says: If the input location (in this case the point of the location you have provided) is not on the top face of the slab, this function will return a null reference. This could be the first issue at a glance. Can you confirm if this point is actually on your floor's top face and try again. Will try run your sample on my end

Carol Gitonga, Developer Advocacy and Support, ADN Open
Message 8 of 12

ahmadkhalaf7892
Advocate
Advocate
Hi Caroline, I am using the same point
XYZ third = new XYZ(20, 15, 0); of the floor, however, I am adding a Z value into it and it is still not working. i would really appreciate the help to know how can I add a point to the floor on the Z elevation.
Thank you
Message 9 of 12

caroline.gitonga
Autodesk
Autodesk

Hi @ahmadkhalaf7892,

Now that I have looked it I have seen that you are working with a sloped floor. Floor.SlabShapeEditor property unfortunately works only on flat and horizontal floor, otherwise, shapeEditor will be a null reference, which you are getting.

Kindly, have a look at this resource and see if you can get some insights: https://jeremytammik.github.io/tbc/a/1121_create_sloped_floor.htm#3 :

Modifying Floor Slope Programmatically – or Not

 

Carol Gitonga, Developer Advocacy and Support, ADN Open
Message 10 of 12

ahmadkhalaf7892
Advocate
Advocate
Hi Caroline, Thanks a lot so basically I have to create a flat floor for myself in order to access the slabshapeEditor, Right? the method That I am using even though it does create a flat and horizontal floor however it is used for the slopped floor.
is there any way I can create the floor programmatically as a flat and then add the points, or is that not possible?
0 Likes
Message 11 of 12

caroline.gitonga
Autodesk
Autodesk
Accepted solution

Hi @ahmadkhalaf7892

I found an issue on your code probably the reason you are getting the exception. You cannot create the floor and then draw points to it within the same transaction. For you to change a Revit model you have to launch a transaction and commit it for the changes to be recorded or persisted: Read More. In this case, for you to create a floor you need to start and commit an independent transaction. Thereafter, if the floor is created successfully you can then access its SlabShapeEditor, then launch another separate transaction to add points to it:

FilteredElementCollector levCollector = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).WhereElementIsNotElementType();
Level lev = levCollector.FirstElement() as Level;
//start transaction to cfreate the floor
Transaction actrans = new Transaction(doc);
actrans.Start("create Floor");
Floor Floor = Floor.Create(doc, new List<CurveLoop> { profile }, floorType.Id, lev.Id);
//committransaction to cfreate the floor
actrans.Commit();


//get floor slab shape editor
SlabShapeEditor slabshapeedit = Floor.SlabShapeEditor;

//start a transaction to draw points on the floor

Transaction trans = new Transaction(doc);

trans.Start("Draw Point on Floor");

slabshapeedit.DrawPoint(new XYZ(0, 15, 15));
//commit transaction to draw points on the floor
trans.Commit();

 

Kindly, try this let me know.

Carol Gitonga, Developer Advocacy and Support, ADN Open
Message 12 of 12

ahmadkhalaf7892
Advocate
Advocate
Thank you very much for your help