PreviousNext
Help > Programming PhotoModeler - Scripting/DDE Interface > Example External Script / DDE Use > Word Macro for PhotoModeler DDE
Word Macro for PhotoModeler DDE

Example program written in Visual Basic for Applications (i.e. VBA for Word and Excel).

 

Sub RunFullDDETest()

    '

    ' Test new DDE Commands in PhotoModeler

    ' Do a project with control points, inverse camera solution and then add

    ' photo, reference points and do bundle adjustment

    '

   

    Dim ret As String

    ' Set up the DDE conversation with PhotoModeler's DDE Server

    '  (note, PhotoModeler program must already be running)

    c = DDEInitiate(App:="PhotoModeler", Topic:="Data")

   

    MsgBox "Starting Project Test"

   

    ' Create empty new project with inches as units

    If Not DDECommand(c, "NewProject 7", ret) Then GoTo Failure

   

    ' Add a camera with guessed at parameters

    If Not DDECommand(c, "AddCamera unknown 8.0 6.7 5.3 640 480 3.5335 2.65", ret) Then GoTo Failure

    If Not DDECommand(c, "GetCamera unknown", ret) Then GoTo Failure

    MsgBox "Starting camera parameters " + ret

    ' Add a photo (no image)

    If Not DDECommand(c, "AddPhoto 1 unknown", ret) Then GoTo Failure

    ' Set the photo to do inverse camera processing to solve for focal length

    ' and format aspect ratio

    If Not DDECommand(c, "SetPhotoProcessing 1 5 1", ret) Then GoTo Failure

    ' Define some fixed control points

If Not DDECommand(c, "DefineControlPoint Box cp1 0.0 2.95 2.9", ret) Then GoTo Failure

    If Not DDECommand(c, "DefineControlPoint Box cp2 0.0 0.0 2.9", ret) Then GoTo Failure

    If Not DDECommand(c, "DefineControlPoint Box cp3 0.0 0.0 0.0", ret) Then GoTo Failure

    If Not DDECommand(c, "DefineControlPoint Box cp8 0.0 2.95 0.0", ret) Then GoTo Failure

    If Not DDECommand(c, "DefineControlPoint Box cp18 2.8 2.95 2.9", ret) Then GoTo Failure

    If Not DDECommand(c, "DefineControlPoint Box cp19 2.8 0.0 2.9", ret) Then GoTo Failure

    If Not DDECommand(c, "DefineControlPoint Box cp20 2.8 0.0 0.0", ret) Then GoTo Failure

    ' Mark these control on photo 1

    If Not DDECommand(c, "MP 1 1 161.768 95.752 Box cp1", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 1 2 283.728 161.240 Box cp2", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 1 3 286.280 297.752 Box cp3", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 1 4 178.720 225.768 Box cp8", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 1 5 278.0 46.0 Box cp18", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 1 6 390.0 92.0 Box cp19", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 1 7 380.0 224.0 Box cp20", ret) Then GoTo Failure

    ' Process the result to do an orientation and inverse camera

    If Not DDECommand(c, "Process 1", ret) Then GoTo Failure

    MsgBox "Orient / Inv. Camera return (should be -1 -1 -1)= " + ret

   

    ' Show the results of the new camera parameters

    If Not DDECommand(c, "GetCamera unknown", ret) Then GoTo Failure

    MsgBox "Solved camera parameters " + ret

   

    ' Add a new photo to use the newly solved camera

    If Not DDECommand(c, "AddPhoto 2 unknown", ret) Then GoTo Failure

    ' Mark and reference some points onto photo 2 that appear as control on photo 1

    If Not DDECommand(c, "MP 2 1 294.24 53.68", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 2 2 400.272 124.76", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 2 3 408.760 227.26", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 2 4 285.288 152.232", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 2 5 218.0 138.0", ret) Then GoTo Failure

   

    ' Mark and reference some new points on photo 1 and 2 that are not control points

    If Not DDECommand(c, "MP 1 8 351.304 340.264", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 2 8 461.248 258.304", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 1 9 342.760 438.248", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 2 9 481.720 390.256", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 1 10 239.768 360.744", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 2 10 356.264 332.232", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 1 11 424.0 356.0", ret) Then GoTo Failure

    If Not DDECommand(c, "MP 2 11 376.0 442.0", ret) Then GoTo Failure

   

    ' Solve the project (which will orient photo 2 and do a bundle adjustment)

    If Not DDECommand(c, "Process 6", ret) Then GoTo Failure

    MsgBox "Processing return = " + ret

   

    ' Get the resulting camera station data

    If Not DDECommand(c, "GetPhotoStation 1 0", ret) Then GoTo Failure

    MsgBox "Station 1 orientation: " + ret

    If Not DDECommand(c, "GetPhotoStation 2 0", ret) Then GoTo Failure

    MsgBox "Station 2 orientation: " + ret

    If Not DDECommand(c, "GetPhotoStation 2 1", ret) Then GoTo Failure

    MsgBox "Station 2 orientation with rot matrix: " + ret

    MsgBox "Finished Project Test"

    GoTo Finished

   

   

Failure:

    MsgBox "Some part of test failed"

   

Finished:

   

    ' Terminate the DDE conversation with PhotoModeler

    DDETerminate c

End Sub

 

' Runs a dde command and returns true if is succeeded.

' If succeed the string ret contains the command return (with the return code missing)

Function DDECommand(chan As Variant, command As String, ret As String) As Boolean

    DDECommand = False

   

    If Len(command) > 254 Then

        MsgBox "<" + command + "> is longer than 254 characters."

        Return

    End If

       

    ' Set up an error handler to trap possible Windows/DDE problems   

    On Error GoTo ErrorReturn

   

    ret = DDERequest(Channel:=chan, Item:=command)

           

    If Left(ret, 1) = "0" Then

        ' command failed

        MsgBox "<" + command + "> failed. Return was <" + ret + ">"

    Else

        ' command succeeded parse off return code

        ret = Mid(ret, 3)

        DDECommand = True

    End If

   

    GoTo OKReturn

   

ErrorReturn:

    MsgBox "<" + command + "> generated an error."

   

OKReturn:

    ' reset error handler   

    On Error GoTo 0

   

End Function