Create a custom Query with Boundary Type as ‘Point’ using Map 3D .NET API

By Partha Sarkar

Presume you have some DWG files attached to your project in AutoCAD Map 3D and you want to run a Query with Boundary Type as 'Point'. What you do in Map 3D? 

You bring up the 'Define Query of Attached Drawing(s)' dialog box and select the 'Location' button. In 'Location Condition' dialog box, select 'Point' (see the screenshot below) and define the location and finally execute the Query. You get to see the desired result in Map 3D Model space.

IMG1

 

IMG2

In a complicated project you don't want user to select the Point location arbitrarily and you simply want them to run a predefined custom command to execute the query with that particular point location. Here is a VB.NET code snippet which uses Map 3D managed API to run a custom query:

'' Create the query using ProjectModel.CreateQuery().
            Dim dwg As Document = Application.DocumentManager.MdiActiveDocument
            Dim database As Database = dwg.Database
            Dim PROJECT As Project.ProjectModel = HostMapApplicationServices.Application.ActiveProject
 
            Dim qryModel As QueryModel = PROJECT.CreateQuery()
            '' Clear the exisitng Query
            qryModel.Clear()
 
            '' Create one or more query conditions
            Dim locationCondition As Query.LocationCondition = New Query.LocationCondition
            Dim qryCondition As LocationCondition = New LocationCondition()
 
            '' This point location is specific to a test DWG file
            Dim pt3d As Point3d = New Point3d(3080168.7995, 1271284.8294, 0.0)
            Dim pntBdry As PointBoundary = New PointBoundary(pt3d)
            qryCondition.Boundary = pntBdry
            Dim qryBranch As New QueryBranch(JoinOperator.OperatorAnd)
            qryBranch.AppendOperand(qryCondition)
 
            ''Create the query definition by passing the root query branch to QueryModel.Define().
            qryModel.Define(qryBranch)
            qryModel.Mode = QueryType.QueryDraw
            qryModel.Run()

Hope this is useful to you!


Comments

3 responses to “Create a custom Query with Boundary Type as ‘Point’ using Map 3D .NET API”

  1. Karl Anich Avatar
    Karl Anich

    hello, i have a problem to run the query (see below) with VB.NET in AutoCad Map 2013 64bit (windows7). The definition of the query works fine, but i am not able to bring it into execution. When i do this step within the programm manually it works fine.
    i would be pleased for an answer, thanks.
    _
    Sub TestQuery()
    Dim prjModel As ProjectModel = HostMapApplicationServices.Application.ActiveProject
    Dim mapApp As Autodesk.Gis.Map.MapApplication = Autodesk.Gis.Map.HostMapApplicationServices.Application
    Dim topoName As String = “Bestände”
    Dim flNr As Integer = 3503
    ‘Topologie laden
    Dim Topos As Topologies = prjModel.Topologies
    Dim TopoBest As TopologyModel = Nothing
    If Topos.Exists(topoName) = True Then
    TopoBest = Topos.Item(topoName)
    Try
    TopoBest.Open(Topology.OpenMode.ForRead)
    Catch MEx As MapException
    If MEx.ErrorCode <> 1002 Then
    System.Windows.MessageBox.Show(“Errorcode: ” & MEx.ErrorCode.ToString & ControlChars.CrLf _
    & “Errormessage: ” & MEx.Message.ToString, “MapException”)
    Exit Sub
    End If
    End Try
    Else
    System.Windows.MessageBox.Show(“Topolgie ist nicht vorhanden!”, “Information”)
    Exit Sub
    End If
    ‘Abfrage
    Dim qryModel As QueryModel = prjModel.CreateQuery(True)
    qryModel.Clear(True)
    qryModel.TopologyName = topoName
    qryModel.Mode = QueryType.QueryDraw
    Dim qryRoot As QueryBranch = QueryBranch.Create()
    Dim DatCond As DataCondition = New DataCondition()
    DatCond.Column = “ID”
    DatCond.ConditionOperator = ConditionOperator.ConditionEqual
    DatCond.DataType = DataQueryType.DataIrd
    DatCond.Flag = True
    DatCond.HasOperatorNot = False
    DatCond.JoinOperator = JoinOperator.OperatorAnd
    DatCond.Table = “TPMCNTR_” & topoName
    DatCond.Value = flNr
    qryRoot.AppendOperand(DatCond)
    qryModel.Define(qryRoot)
    ‘Eigenschaften
    Dim propChange As PropertyAlterationDefinition = qryModel.PropertyAlteration
    Dim propColor As PropertyAlteration = Nothing
    propColor = propChange.AddAlteration(AlterationType.AlterationColor)
    propColor.Expression = 6
    qryModel.EnablePropertyAlteration(True)
    ‘Ausführen
    If qryModel.Defined Then
    Try
    qryModel.Run()
    Catch Mex As MapException
    System.Windows.MessageBox.Show(“Errorcode: ” & Mex.ErrorCode.ToString & ControlChars.CrLf _
    & “Errormessage: ” & Mex.Message.ToString, “MapException”)
    End Try
    End If
    End Sub

  2. Hi Karl,
    Is it specific to any DWG file ? Do you get any error ?
    Unless I debug with that DWG file, it hard to say why it’s not showing any result.
    Thanks,
    Partha

  3. Karl Anich Avatar
    Karl Anich

    Hi Partha,
    thank you for quick response!
    The problem is not specific to a DWG. You can test it with any DWG using a topology. No error message is reported. I debugged it with a test DWG and topology (named test) and defined the query as shown below. The query (property change) is defined in the drawing but I have to execute it manually.
    _
    Sub TestQuery()
    Dim prjModel As ProjectModel = HostMapApplicationServices.Application.ActiveProject
    Dim mapApp As Autodesk.Gis.Map.MapApplication = Autodesk.Gis.Map.HostMapApplicationServices.Application
    Dim topoName As String = “Test”
    Dim flNr As Integer = 9
    ‘Topologie laden
    Dim Topos As Topologies = prjModel.Topologies
    Dim TopoBest As TopologyModel = Nothing
    If Topos.Exists(topoName) = True Then
    TopoBest = Topos.Item(topoName)
    Try
    TopoBest.Open(Topology.OpenMode.ForRead)
    Catch MEx As MapException
    If MEx.ErrorCode <> 1002 Then
    System.Windows.MessageBox.Show(“Errorcode: ” & MEx.ErrorCode.ToString & ControlChars.CrLf _
    & “Errormessage: ” & MEx.Message.ToString, “MapException”)
    Exit Sub
    End If
    End Try
    Else
    System.Windows.MessageBox.Show(“Topolgie ist nicht vorhanden!”, “Information”)
    Exit Sub
    End If
    ‘Abfrage
    Dim qryModel As QueryModel = prjModel.CreateQuery(True)
    qryModel.Clear(True)
    qryModel.TopologyName = topoName
    qryModel.Mode = QueryType.QueryDraw
    Dim qryRoot As QueryBranch = QueryBranch.Create()
    Dim DatCond As DataCondition = New DataCondition()
    DatCond.Column = “ID”
    DatCond.ConditionOperator = ConditionOperator.ConditionEqual
    DatCond.DataType = DataQueryType.DataIrd
    DatCond.Flag = True
    DatCond.HasOperatorNot = False
    DatCond.JoinOperator = JoinOperator.OperatorAnd
    DatCond.Table = “TPMCNTR_” & topoName
    DatCond.Value = flNr
    qryRoot.AppendOperand(DatCond)
    qryModel.Define(qryRoot)
    ‘Eigenschaften
    Dim propChange As PropertyAlterationDefinition = qryModel.PropertyAlteration
    Dim propColor As PropertyAlteration = Nothing
    propColor = propChange.AddAlteration(AlterationType.AlterationColor)
    propColor.Expression = 6
    qryModel.EnablePropertyAlteration(True)
    ‘Ausführen
    If qryModel.Defined Then
    Try
    qryModel.Run()
    Catch Mex As MapException
    System.Windows.MessageBox.Show(“Errorcode: ” & Mex.ErrorCode.ToString & ControlChars.CrLf _
    & “Errormessage: ” & Mex.Message.ToString, “MapException”)
    End Try
    End If
    End Sub
    Thanks,
    Karl

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading