Creating a query: sample code

AutoCAD Map 3D ActiveX

Creating a query

The following code queries objects from a source file.

Public Sub queryexample()

 

Dim amap As AcadMap

Dim prj As Project

Dim qry As Query

Dim mainqrybr As QueryBranch

Dim qrylf As QueryLeaf

Dim propqrylf As QueryLeaf

Dim dataqrylf As QueryLeaf

Dim andqrybr As QueryBranch

Dim mapu As MapUtil

Dim wind As WindowBound

Dim boolVal As Boolean

Dim atdr As AttachedDrawing

Dim dblary As Variant

 

'Get AutoCAD Map and Project Objects

Set amap = ThisDrawing.Application. _

GetInterfaceObject("AutoCADMap.Application") 

Set prj = amap.Projects(ThisDrawing)

 

'Get Current Query

Set qry = prj.CurrQuery

qry.Clear

 

'Attach DWG

Set atdr = prj.DrawingSet.Add("MAPTUT:\\citymap7.dwg")

 

'Get Main Branch

Set mainqrybr = qry.QueryBranch

 

'Create Layout of Leaves and Branch

Set qrylf = mainqrybr.Add(kLocationCondition, kOperatorAnd)

 

'Set Main Branch

Set andqrybr = mainqrybr.Add(kQueryBranch, kOperatorAnd)

 

' Set Conditions for Leaves

Set propqrylf = andqrybr.Add(kPropertyCondition, kOperatorOr)

Set dataqrylf = andqrybr.Add(kDataCondition, kOperatorOr)

 

'Get DWG Extents

dblary = prj.DrawingSet.Item("MAPTUT:\\citymap7.dwg").Extents

 

'Define Boundary Area for Location

Set mapu = prj.MapUtil

Set wind = mapu.NewWindow( _

mapu.NewPoint3d(dblary(0), dblary(1), 0), _

mapu.NewPoint3d(dblary(2), dblary(3), 0))

 

'Complete Leaves

boolVal = qrylf.SetLocationCond(kLocationInside, wind)

boolVal = propqrylf.SetPropertyCond(kLayer, kCondEq, "Stream")

boolVal = dataqrylf.SetDataCond( _

kDataIRD, _ 

kCondLT, _ 

"Water_Bodies", _ 

"Avg_Depth", _ 

10) 

 

'Specify Draw Query

qry.Mode = kQueryDraw

 

'Define Query

boolVal = qry.Define(mainqrybr)

 

'Execute Query

boolVal = qry.Execute

ThisDrawing.Application.ZoomExtents

End Sub