Challenge: Display all user labels associated with spatial elements in a confined cube.
Summary
By completing this you will learn how to:
- Create a new imodel to be used with the imodel console.
- Query for specific properties in an ECClass.
- Join classes for additional information.
- Add conditions to find specific elements.
Setup
- Go to the getting started registration dashboard (If you haven’t registered yet, please register a new account)
- Click on “New iModel”
- Name the imodel whatever you like (i.e. “My House Sample”).
- In the drop down menu, select “House Sample”.
- Click on “submit” and allow the process to complete.
- Navigate to the imodel console.
- Follow the sign in process to gain access to the console.
- (Optional): Read through the quick tips and do the tutorial.
- (Optional): If you’re not familiar with ECSQL/SQL, check out our learning page
Instructions
Once the setup is complete, we’ll need to open the imodel you’ve just cloned using the imodel console.
Opening your imodel in the console.
- Click on your project name in the table under “Listing all Contexts” to display imodels associated with the project
- Click on your imodel name in the table under “Listing iModels from Project” to display changets associated with the project
- Click on the named version id to finally open the imodel.
Congratulations! The imodel is now open and ready for queries.
Steps to complete the challenge
1) Begin by listing all EC properties available for all spatial elements in the imodel.
- Write a query to pull all ECProperties from ECClass
Bis.SpatialElement
- The qualifier for all properties is ‘
*
’
2) Instead of displaying all EC properties, write a query that only lists the EC properties we’re interested in: “ECInstanceId”, “UserLabel” and “Origin”
- ECInstanceId is the
unique
identifier for the element. - UserLabel is a user created label to describe the element.
- Origin will tell us where the element is in the 3d space.
3) While the origin locates where the spatial element is, the size is still unknown. We need to query from the class “Bis.SpatialIndex” for this.
- Write a query (similar to step 1) that lists all information available from the class
Bis.SpatialIndex
- Class Bis.SpatialIndex contains range information for spatial elements
4) Notice how SpatialIndex contains the range but it doesn’t contain ‘UserLabel’. We need to JOIN
class Bis.SpatialElement
and class Bis.SpatialIndex
together.
- Write a query to to display all EC properties from class
Bis.SpatialElement
andBis.SpatialIndex
together. - To join the classes, we need to find the key ECProperty that both classes can
JOIN
on.
5) Once again the data shown is too verbose. Only pull the information we want to see.
- Write a query to pull only
UserLabel
fromBis.SpatialElement
andMinX, MinY, MinZ, MaxX, MaxY, MaxZ
fromBis.SpatialIndex
- TIP: We can alias names - instead of writing
bis.SpatialElement.ECInstanceId
, we can writee.ECInstanceId
if we declareSELECT ... FROM bis.SpatialElement e
6) Now that we have all the information we need, it’s time to add a simple condition.
- Write a query with the WHERE clause to show only spatial elements that have a MinX >= 5 AND MinY >= 6.
- The format for WHERE clause is
FROM ... WHERE ... AND ...
7) The final challenge is to retrieve the user labels of all spatial elements that are contained in a cube with the minimum bounding coordinate at (5, 6, 6) and maximum bounding coordinate at (15, 15, 14).
- (X, Y, Z) are cartesian coordinates.
Conclusion
So, were you up to the challenge? Feel free to explore the console and experiment with your own queries. You can learn much more about how ECSQL queries work here.
Feedback is welcome! Let us know via the iModel.js community.