Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to create a viewpoint 1

Status
Not open for further replies.

AlexMorasan

Automotive
Aug 17, 2020
13
0
0
MX
Hello everyone

I have to create a macro that will allow the user to create an specific viewpoint of the main assembly so the user can take an screenshot ( the screenshot is use to update our assembly handbook).
I have analyzed the DMUInfEngineeringView file and with that I have created my own code.
Language="VBSCRIPT"

Sub CATMain()

Dim ProjectionM
Dim FocusD
FocusD = 837.729

' Get the viewer
Set oViewer = CATIA.ActiveWindow.ActiveViewer

' Get the viewpoint
Set oViewpoint = oViewer.Viewpoint3D

Dim origin(2)
Dim sight(2)
Dim up(2)

M = 2568.767
N = -301.194
P = 323.169

X = 0.972
Y = 0.130
Z = 0.195

U = -0.200
V = 2.627
W = 0.979

' Change the viewpoint

oViewpoint.PutUpDirection Array(U, V, W)
oViewpoint.PutOrigin Array(M, N, P)
oViewpoint.PutSightDirection Array(X, Y, Z)

oViewpoint.ProjectionMode = catProjectionCylindric
ProjectionM = oViewpoint.ProjectionMode
MsgBox ProjectionM

oViewpoint.FocusDistance = FocusD

' Update the viewer
oViewer.Update

oViewpoint.GetOrigin origin
oViewpoint.GetSightDirection sight
oViewpoint.GetUpDirection up

For i = 0 to 2
MsgBox origin(i)
Next

For i = 0 to 2
MsgBox sight(i)
Next

For i = 0 to 2
MsgBox up(i)
Next

Set oViewpoint = Nothing
Set oViewer = Nothing

End Sub

However my problem is that instead of getting the viewpoint that I need I always get a rotated version of my desired viewpoint.

Example:

This is the viewpoint that I need, I already know the coordinates for the origin, the sight direction, the up direction and the focus distance (I use another macro to get this data)

objective_kbyqwu.png


This is the viewpoint that I get, as you can see the viewpoint is slightly rotated.

result_txdl8w.png


Currently the code is in CATscript but the final version will be in VBA.

I would really appreciate any help you can give me.
Thanks in advance for your support.

Alex
 
Replies continue below

Recommended for you

Hello Little Cthulhu

Thank you for taking the time to answer my post.

Yes I have tried in another assemblies but I still get a different version from my objective viewpoint.
Right now I am using CATIA Version 5-6 Release 2018 Service Pack 3
 
Are you sure about the way you retrieve your hard-coded values?

I can't reproduce it in R25. I ran script below up to breakpoint, moved sight and then continued execution.
Viewpoint returned back to initial position as expected.

Code:
Option Explicit

Sub CATMain()
    Dim v3d As Viewer3D: Set v3d = CATIA.ActiveWindow.ActiveViewer
    Dim vp: Set vp = v3d.Viewpoint3D
    
    Dim fd: fd = vp.FocusDistance
    Dim origin(2), sight(2), up(2)
    vp.GetOrigin origin
    vp.GetSightDirection sight
    vp.GetUpDirection up
    Dim zm: zm = vp.Zoom
    
    ' BREAKPOINT HERE
    vp.FocusDistance = fd
    vp.PutUpDirection up
    vp.PutSightDirection sight
    vp.PutOrigin origin
    vp.Zoom = zm
    v3d.Update
End Sub
 
Hello everyone

My apologies for taking so long to answer but because of COViD-19 we weren't allowed to go to the office and so I was unable to work on this macro.
Nevertheless I am finally back and I have manage to correct my macro.

Unfortunately I don't know the reason but every time I run the macro the UP Direction vector changes , so my solution was ( probably not the best solution but it works) to modify a second time the UPDirection to correct the viewpoint.

The main part of the code related to the viewpoints is like this:

Option Explicit

Sub CATMain()
Dim v3d As Viewer3D: Set v3d = CATIA.ActiveWindow.ActiveViewer
Dim vp: Set vp = v3d.Viewpoint3D

Dim fd: fd = vp.FocusDistance
Dim origin(2), sight(2), up(2)
vp.GetOrigin origin
vp.GetSightDirection sight
vp.GetUpDirection up
Dim zm: zm = vp.Zoom

' BREAKPOINT HERE
vp.FocusDistance = fd
vp.PutUpDirection up
vp.PutSightDirection sight
vp.PutOrigin origin
vp.Zoom = zm

'In this part I modify for a second time the up direction to correct the viewpoint.
vp.PutUpDirection up

v3d.Update
End Sub

thank you for your support and once again my apologies for letting this thread open for so long.

Alex
 
Thank you for the star.
The main idea of this macro was to create a matrix of viewpoints in excel to make it easier to update the assembly handbook that we deliver to our client.

So, if anybody is in need of something similar I will be happy to share my code with you.
Please let me know if you need a sample of the code.
 
Hi AlexMorasan,
I am working on a task to get snips of four views (ISO, Front, Top, Side)of the product from CATIA.
I need to do it in CAA, but your vb code will help me get the math right.
Can you share the sample code with me?

Thanks for the help.
 
Hi Sardeshmukh

My apologies for answering until now, but I was on vacation and I didn't see my mail until now.
Of course I can provide you with the code, just tell me which part you are interested in ? the part to acquire the viewpoints or the part to change the viewpoint?
I am sorry if I can't share all the code but it contains information from my company that I can share I hope you understand.
Promise I will give you the information as soon as you reply.

By the way I have a question, what is CAA?

Thanks in advance I am looking forward for your answer
 
Hi AlexMorasan,
Thanks for the reply. I am interested in the code for changing the viewpoints.
CAA is a framework based on C++ which is used to customize CATIA.
Ex: CAA can be used to create toolbars, commands, one can deal with mechanical modelling, geometrical modelling etc.

Thanks again for the help
 
Hello Sardeshmukh

Here is the part of the code, I hope it will be useful for you.
The macro that I made uses an userinterface, in this UI the user have to write the viewpoint that he needs (this viewpoints are related to the pictures of the assembly handbook). The coordinates of that viewpoints are stored in an excel file, so the macro only have to look for that specific viewpoint and retrieve the information.

I hope the code is understandable but if you have any question please feel free to ask, I will try to answer ASAP

Private Sub Go_ToFile_Click()
Dim v3d As Viewer3D
Set v3d = CATIA.ActiveWindow.ActiveViewer
Dim vp
Set vp = v3d.Viewpoint3D
Dim fd
Dim origin(2), sight(2), up(2)

Dim last_pos As Integer
Dim stock_pos As Integer
Dim page As String
Dim stock_exist As Boolean


Dim PDM_file As String
Dim PDM_Project As String

stock_pos = 0
stock_exist = False
page = PDM_Page.Value

PDM_Project = Project.Value

Dim oExcel As Excel.Application
Set oExcel = CreateObject("Excel.Application")

Select Case PDM_Project


Case Is = "XXX"
'---Launch Excel---
On Error Resume Next
'if Excel is not open then Launch Excel

oExcel.Workbooks.Open ("XXXXX")

'
Case Is = "XXX"
'---Launch Excel---
On Error Resume Next
'if Excel is not open then Launch Excel

oExcel.Workbooks.Open ("XXXXX")
End Select

PDM_file = PDM_Zone.Value
oExcel.Worksheets(PDM_file).Activate



oExcel.Visible = False

On Error GoTo 0
'get the curren Excel sheet
Dim OWB As Excel.Workbook

'If there is already a workbook, set it as active, otherwise create a new workbook
If oExcel.Workbooks.Count <> 0 Then
Set OWB = oExcel.ActiveWorkbook
Else
Set OWB = oExcel.Workbooks.Add
End If

last_pos = oExcel.Cells(1, 15)



For stock = 1 To last_pos
If page = oExcel.Cells(stock, 1) Then
stock_exist = True
stock_pos = stock
Exit For
Else
stock_exist = False
End If
Next stock

If stock_exist = True Then
For origin_index = 0 To 2
origin(origin_index) = oExcel.Cells(stock_pos, origin_index + 2)
Next origin_index

For sight_index = 0 To 2
sight(sight_index) = oExcel.Cells(stock_pos, sight_index + 5)
Next sight_index

For up_index = 0 To 2
up(up_index) = oExcel.Cells(stock_pos, up_index + 8)
Next up_index

fd = oExcel.Cells(stock_pos, 11)
zm = oExcel.Cells(stock_pos, 12)

vp.FocusDistance = fd
vp.PutUpDirection up
vp.PutSightDirection sight
vp.PutOrigin origin
vp.Zoom = zm
v3d.Update
vp.PutUpDirection up
v3d.Update
Else
MsgBox "That page does not exists yet, please another page"
End If
oExcel.Workbooks.Close

Set oExcel = Nothing

End Sub

Do you know some bibliography where I can learn more about CAA? or an specific link? it seems interesting and I would like to improve my macros.

Alex
 
Status
Not open for further replies.
Back
Top