The following example queries objects from a source file.
Attach source drawings
Attach the source drawings that you will query from. For example, to attach citymap7.dwg, whose path is represented by the drive alias, MAPTUT, use this code.
Dim atdr As AttachedDrawing
Set atdr = prj.DrawingSet.Add("MAPTUT:\\citymap7.dwg")
Create the main branch
1 Get the AutoCAD Map application.
Dim amap As AcadMap
Set amap = ThisDrawing.Application. _
GetInterfaceObject("AutoCADMap.Application")
2 Get the project and the current query.
Dim prj As project
Dim qry As Query
Set prj = amap.Projects(ThisDrawing)
Set qry = prj.CurrQuery
3 Clear the current query's main branch (its definition). Note If you plan to use this query again, save a copy of it in the query library before clearing it. Otherwise, it will be lost.
qry.Clear
4 Get a copy of the main branch, which is now empty.
Dim mainqrybr As QueryBranch
Set mainqrybr = qry.QueryBranch
5 Add a location, property, data, or SQL condition to the branch. For example, create a location query to retrieve all objects inside a window.
Dim qrylf As QueryLeaf
Set qrylf = mainqrybr.Add(kLocationCondition, kOperatorAnd)
6 Define and set the boundary for the location leaf.
Dim wind As WindowBound
Dim boolVal As Boolean
Dim mapu As MapUtil
Dim dblary As Variant
dblary = prj.DrawingSet.Item("MAPTUT:\\citymap7.dwg").Extents
Set mapu = prj.MapUtil
Set wind = mapu.NewWindow( _
mapu.NewPoint3d(dblary(0), dblary(1), 0), _
mapu.NewPoint3d(dblary(2), dblary(3), 0))
boolVal = qrylf.SetLocationCond(kLocationInside, wind)
Create sub-branches and leaves
1 Create other leaves and branches. For example, create an AND sub-branch with property and data condition leaves.
Dim andqrybr As QueryBranch
Set andqrybr = mainqrybr.Add(kQueryBranch, kOperatorAnd)
2 Set the conditional relationships for the leaves. For example, to the AND sub-branch, add OR leaves.
Set propqrylf = andqrybr.Add(kPropertyCondition, kOperatorOr)
Set dataqrylf = andqrybr. .Add(kDataCondition, kOperatorOr)
3 Set the property and object data leaves to query objects within a window boundary and that are either on the stream layer or a water body with an average depth of less than 10 feet.
boolVal = propqryleaf.SetPropertyCond(kLayer, kCondEq, "Water")
boolVal = dataqryleaf.SetDataCond _
(kDataIRD, kCondLT, "Water_Bodies", "Avg_Depth", 10)
4 Redefine the query (assign the branch that you copied and modified as the new main branch).
boolVal = qry.Define(mainbr)
5 Execute the query.
boolVal = qry.Execute