How to get the bounding box from a Revit Element with Revit API, then call to center of that bounding box - bounding-box

I am trying to rotate Revit elements about their center points. In order to do that, I need to select a Revit element and find its center point, then create a line with the coordinates at that elements center point.
My best idea to accomplish this is to wrap a Revit element in a bounding box and then find the center of that box. My problem is that I am unsure how to accomplish this.
I am using pyRevit (amazing tool) and I am stuck on how to either wrap the selected element with a bounding box or retrieve its existing bounding box.
Any help would be greatly appreciated! I am really trying to learn the Revit API and understand how everything works. I am making progress but there is a lot to unpack.
def pickobject():
from Autodesk.Revit.UI.Selection import ObjectType
#define the active Revit application and document
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
#define a transaction variable and describe the transaction
t = Transaction(doc, 'This is my new transaction')
# Begin new transaction
t.Start()
# Select an element in Revit
picked = uidoc.Selection.PickObject(ObjectType.Element, "Select something.")
### ?????????? ###
# Get bounding box of selected element.
picked_bb = BoundingBoxXYZ(picked)
# Get max and min points of bounding box.
picked_bb_max = picked_bb.Max
picked_bb_min = picked_bb.Min
# Get center point between max and min points of bounding box.
picked_bb_center = (picked_bb_max + picked_bb_min) / 2
### ?????????? ###
# Close the transaction
t.Commit()
return picked, picked_bb_center
Thanks in advance for taking a look at what I have so far. Please let me know if anything needs further clarification!
edit:
#CyrilWaechter
I think you are right. Using LocationPoint would probably make more sense. I looked through the script you linked (thank you btw!) and I tried implementing this section in my code.
transform = doc.GetElement(picked.ElementId).GetTransform()
I am passing the ElementId through this statement but I get the error, "Wall" object has no attribute 'GetTransform'. Could you please help me understand this?
edit 2:
Thanks #JeremyTammik and #CyrilWaechter, your insights helped me understand where I was going wrong. While I still feel that certain properties are ambiguous in the Revit API, I was able to get my code to execute properly. I will post the code that I was able to get working below.

The centre of the bounding box is very easy to obtain. picked is a Reference. Get the ElementId from that, open it using doc.GetElement, and retrieve the bounding box using get_BoundingBox, cf. Conduits Intersecting a Junction Box
:
Element e = Util.SelectSingleElement(
uidoc, "a junction box" );
BoundingBoxXYZ bb = e.get_BoundingBox( null );
For certain elements and certain irregular shapes, you might want to use the centroid instead of the bounding box:
Solid Centroid and Volume Calculation
GetCentroid on GitHub

Edited and preserved for posterity by The Building Coder:
Python Rotate Picked Around Bounding Box Centre
Many thanks to Christian for the interesting discussion and Cyril for the wealth of additional information he provides!

Here is how I was able to solve my problem using pyRevit. This code allows you to rotate an element about its Z axis from the center of its bounding box.
To use this code, select a single Revit element and then open the Revit Python Shell. Copy and paste the code below into the Revit Python Shell notepad and click the run button. This will rotate the element by 45 degrees because the current rotateSelectedElement() argument is 45. You may change this number to any value before running.
# Import the math module to convert user input degrees to radians.
import math
# Get a list of all user selected objects in the Revit Document.
selection = [doc.GetElement(x) for x in uidoc.Selection.GetElementIds()]
# Definitions
def rotateSelectedElement(degrees_to_rotate):
from Autodesk.Revit.UI.Selection import ObjectType
#define the active Revit application and document
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
#define a transaction variable and describe the transaction
t = Transaction(doc, 'This is my new transaction')
# Convert the user input from degrees to radians.
converted_value = float(degrees_to_rotate) * (math.pi / 180.0)
# Begin new transaction
t.Start()
# Get the first selected element from the current Revit doc.
el = selection[0].Id
# Get the element from the selected element reference
el_ID = doc.GetElement(el)
# Get the Bounding Box of the selected element.
el_bb = el_ID.get_BoundingBox(doc.ActiveView)
# Get the min and max values of the elements bounding box.
el_bb_max = el_bb.Max
el_bb_min = el_bb.Min
# Get the center of the selected elements bounding box.
el_bb_center = (el_bb_max + el_bb_min) / 2
#Create a line to use as a vector using the center location of the bounding box.
p1 = XYZ(el_bb_center[0], el_bb_center[1], 0)
p2 = XYZ(el_bb_center[0], el_bb_center[1], 1)
myLine = Line.CreateBound(p1, p2)
# Rotate the selected element.
ElementTransformUtils.RotateElement(doc, el, myLine, converted_value)
# Close the transaction
t.Commit()
# Execute
# Add the desired degrees to rotate by as an argument for rotateSelectedElement()
rotateSelectedElement(45)
edit: Made code clearer. Code now executes in Revit Python Shell without any further modifications. Refer to directions above if you have trouble!

Related

polyline.encode strange format

I am working with the polyline of google, I would like to give a set of coordinates and generate the correct polyline and viceversa. In particular in the end i would like to url encode the result (the polyline).
When I insert a polyline like:
code = '%28%28akntGkozv%40kcCka%40us%40y%7BDfvAm%7BBnuCj_Aus%40fzG%29%29'
I use the polyline package: https://pypi.org/project/polyline/, and first I decode the polyline in order to see the coordinates:
coordinates = polyline.decode(code)
print(coordinates)
>> [(3e-05, -0.0001), (-0.0001, -7e-05), (-0.0002, -0.0002), (45.46221, 35.36626), (45.4621, 35.36617), (45.48328, 35.39727), (45.48317, 35.39718), (45.5172, 35.39707), (45.51711, 35.39816), (45.51723, 35.39814), (45.5172, 35.38418), (45.51823, 35.3843), (45.51821, 35.38428), (45.49413, 35.37398), (45.52816, 35.37387), (45.52807, 35.32855), (45.5281, 35.32845), (45.52823, 35.32848), (45.52813, 35.32861)]
and everything here is fine, the problems comes when I try to encode the coordinates back to the polyline (which is my ultimate goal since in the end i would like to give some coordinates and obtain the corresponding polyline)
new_code = polyline.encode(coordinates)
print(new_code)
>> ERXERXakntGkozvETPkcCkaETPusETPyEWBDfvAmEWBBnuCj_AusETPfzGERYERY
Which is slightly different from the original and if put back in the url it doesnt work!
So my question here are:
what kind of encoding is new_code? I have tried to encode it in percentage url using urllib.parse.quote(new_code) but the result is exactly the same, maybe I neeed to specify some particular encoding style but i didnt found anything.
The polyline that I used is a square inside the city of Milan (so only 4 points, maximum 5, are required to identify this area), but the coordinates results from the polyline.decode gives me back a list with 19 points with coordinates that are not even close to the city of Milan. Why?
Ok so basically all of my problems came from the fact that the string i was considering: %28%28akntGkozv%40kcCka%40us%40y%7BDfvAm%7BBnuCj_Aus%40fzG%29%29
contains %28%28 and %29%29 which are not part of the polyline but are simply two (( and )) inserted by the particular url of the site I was using. A simple replace and an encode return the correct polyline:
code = '%28%28akntGkozv%40kcCka%40us%40y%7BDfvAm%7BBnuCj_Aus%40fzG%29%29'
code = code.replace('%28', '').replace('%29', '')
code = urllib.parse.unquote(code)
print(code)
>> irotG_hzv#woBmE}i#yjE`oBwkDf|ChRhMzeG}~BxcB
Which infact, if put inside the polyline.decode returns exactly the coordinates that I have used:
coordinates = polyline.decode(code)
print(coordinates)
>> [(45.46869, 9.15088), (45.48673, 9.15191), (45.4936, 9.18452), (45.47567, 9.21216), (45.45051, 9.20907), (45.44822, 9.16701), (45.46869, 9.15088)]
Which are exactly 7 (now i have changed the shape so a sixtagon instead of a square) and points exactly in the city of Milan

Buffer country borders with st_buffer and a SpatialPolygonsDataFrame?

I'm using coordinate_cleaner's country test cc_coun but it's flagging entries with coordinates near to the edges country borders. To try to keep them I wanted to buffer the terrestrial area of countries, essentially expanding their borders a little so that it doesn't flag these entries.
I've tried:
require(rnaturalearth)
world <- ne_countries(scale = "large", returnclass = "sf") %>% st_buffer(dist=.001)
Using st_buffer(dist=.001) does change the geometry, but I noticed whatever I put into dist doesn't matter as it changes it to the same thing regardless (I tried .001, 0.1, 1, 2, and -1, though any minus number removes the geometry altogether).
I found that maybe I need to transform my sf file into something else and then use st_buffer so it's in meters rather than degrees(?). I saw this question/answer but I don't understand it well enough to help my situation: What unit is the `dist` argument in `st_buffer` set to by default?
In the end I'm looking to create a SpatialPolygonsDataFrame reference file to feed into cc_coun. Using the above code I followed on with:
world <- sf:::as_Spatial(world)
coun_test <- cc_coun(x = data,
lon = "Decimal_Long",
lat = "Decimal_Lat",
iso3 = "Country_code",
value = "flagged",
ref = world,
verbose = TRUE)
Which ended up flagging more entries than when I didn't use st_buffer on the reference fine.
In summary, I want to add a buffer to the edge of every country border by around 100 meters in a file I can use as a reference in this test. Is this the best way to go about it or is there a better/easier way? I'd appreciate any advice.
Thank you

Format Altair choropleth map legend scale and tooltip

I am trying to make the tool tip 'Percentage' be an actual percent and not a decimal. Even when I include alt.Tooltip('Percentage:Q',format='.2%'), it doesn't seem to work.
Also, I am trying to make the legend scale from 0-100% instead of 40-70%.
Any help would be appreciated!
import altair as alt
from vega_datasets import data
states = alt.topo_feature(data.us_10m.url, 'states')
variable_list = ['Percentage', 'State Name', 'state_id']
alt.Chart(states).mark_geoshape().encode(
color=alt.Color('Percentage:Q', title='Positive NFB', legend=alt.Legend(format=".0%"), scale=alt.Scale(scheme='yellowgreen')),
tooltip=['State Name:N', 'Percentage:Q', alt.Tooltip('Percentage:Q',format='.2%')]).properties(title="Percentage of People in Households with Positive NFB"
).transform_lookup(
lookup='id',
from_=alt.LookupData(states_positive_NFB, 'state_id', variable_list)
).properties(
width=500,
height=300
).project(
type='albersUsa'
)
Current map:
To change the domain of the color scale, you can pass the domain argument to alt.Scale(): e.g.
alt.Scale(scheme='yellowgreen', domain=[0, 1])
To make the tooltip format appear, you can remove the duplicated tooltip encoding, as the first one appears to be taking precedence. That is, rather than
tooltip=['State Name:N', 'Percentage:Q', alt.Tooltip('Percentage:Q',format='.2%')]
you should use
tooltip=['State Name:N', alt.Tooltip('Percentage:Q', format='.2%')]

UK geojson displays blank folium map

I'm trying to use the following geojson for UK local authorities, but it displays a blank map.
<https://martinjc.github.io/UK-GeoJSON/json/eng/topo_lad.json -O UK_counties.json>
My table lists most of the local authorities in the same way. Not sure if its the key_on reference that's the only problem or if there's more to it.
My code so far is below:
threshold_scale = np.linspace(df_ctbl['Population'].min(), df_ctbl['Population'].max(), 6, dtype=int)
threshold_scale = threshold_scale.tolist() # change the numpy array to a list
threshold_scale[-1] = threshold_scale[-1] + 1 # make sure that the last value of the list is greater than the maximum immigration
# let Folium determine the scale.
londonf_map = folium.Map(location=[latitudeld, longitudeld], zoom_start=4, tiles='Mapbox Bright')
londonf_map.choropleth(
geo_data=uk_geo,
data=df_ctbl,
columns=['Local Authorityp', 'Population'],
key_on= 'properties.lad13nm',
threshold_scale=threshold_scale,
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='UK Population in 2019',
reset=True
)
londonf_map
Can anyone help? Thanks
You can have a look at this answer : question about Mapbox bright. I think you have a problem with the tiles name "Mapbox Bright".

Parameter "triangulatedPoints" of recoverPose and math behind it

cv::recoverPose has parameter "triangulatedPoints" as seen in documentation, though math behind it is not documented, even in sources (relevant commit on github).
When I use it, I get this matrix in following form:
[0.06596200907402348, 0.1074107606919504, 0.08120752154556411,
0.07162400555712592, 0.1112415181779849, 0.06479560707001968,
0.06812069103377787, 0.07274771866295617, 0.1036230973846902,
0.07643884790206311, 0.09753859499789987, 0.1050111597547035,
0.08431322508162108, 0.08653721971228882, 0.06607013741719928,
0.1088621999959361, 0.1079215237863785, 0.07874160849424018,
0.07888037486261903, 0.07311940086190356;
-0.3474319603010109, -0.3492386196164926, -0.3592673043398864,
-0.3301695131649525, -0.3398606744869519, -0.3240186574427479,
-0.3302508442361889, -0.3534091474425142, -0.3134288005980755,
-0.3456284001726975, -0.3372514921152191, -0.3229005408417835,
-0.3156005118578394, -0.3545418178651592, -0.3427899760859008,
-0.3552801904337188, -0.3368860879000375, -0.3268499974874541,
-0.3221050630233929, -0.3395139819250934;
-0.9334091581425227, -0.9288726274060354, -0.9277125424980246,
-0.9392374374147775, -0.9318967835907961, -0.941870018271934,
-0.9394698966781299, -0.9306592884695234, -0.9419749503870455,
-0.9332801148509925, -0.9343740431697417, -0.9386198310107222,
-0.9431781968459053, -0.9290466865633286, -0.9351167772249444,
-0.9264105322194914, -0.933362882155191, -0.9398254944757025,
-0.9414486961893244, -0.935785675955617;
-0.0607238817598344, -0.0607532477465341, -0.06067768097603395,
-0.06075467523485482, -0.06073245675798231, -0.06078081616640227,
-0.06074754785132623, -0.0606879948481664, -0.06089198212719162,
-0.06071522666667255, -0.06076842109618678, -0.06083346023742937,
-0.06084805655000008, -0.0606931888685702, -0.06071558440082779,
-0.06073329803512636, -0.06078189449161094, -0.06080195858434526,
-0.06083228813425822, -0.06073695721101467]
e.g. 4x20 matrix (in this case there were 20 points). I want to convert this data to std::vector in order to use it in solvePnP. How to do it, what is the math here? Thanks!
OpenCV offers a triangulatePoints function, which has the same output:
points4D 4xN array of reconstructed points in homogeneous coordinates.
Which indicates that each column is a 3D point in homogeneous coordinate system. However your points looks quite not as I would expect. For instance your first point is:
[0.06596200907402348, -0.3474319603010109, -0.9334091581425227, -0.0607238817598344]
But I would expect the last component to be 1.0 already. You should double check if something is not wrong here. You can always remove the "scaling" of the point by dividing each dimension by the last component:
[ x, y z, w ] = w [x/w, y/w, z/w, 1]
And then use the first three parts for your PnP solution.
I hope this helps

Resources