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!

Controlling when a userform is opened

Status
Not open for further replies.

LucasC

Automotive
Feb 18, 2019
157
0
0
US
I've been struggling to find a -reliable- solution to this problem for over a year now.

I only want my userform/macro to open in V5 if an active document is present. Otherwise, if no files are open, the macro gives a runtime error for the user.

Currently, I control this with doing a simple If CATIA.Documents.Count = 0 then msg to user and exit sub. works completely fine right after you start CATIA and no files have been opened. Here is where it gets tricky. after working/opening/closing files there are .CATfct files present in the background that apparently don't count as active documents. They do, however, increase the value of CATIA.documents.count so my simple if..then = 0 logic fails since the count is now a value 1-4 with no active documents open.

There seems to be different types that occur in our system ABQMaterialscatalog.CATfct, CAAPstProductIconExt.CATfct, and another 1 or 2 I can't duplicate atm (QWERTY....something or CATSessionSetup...something rings a bell). these seem to occur randomly depending on what files/products have been worked on by the user. so it can be 0-3or4 of them present in any combination.

Any ideas how I can make this function properly? catia.activedocument doesn't get the .count property unfortunately.

I've been able to capture the .CATfct files in an array and identify them when present. I tried the below code for Product/part/drawing with no luck. But like I mentioned before, there may be 0, 1, 2, 3, 4 or more present so the array goes out of range when they are not present. I also think my use of the "<> right(string,8)" is wrong...

Code:
Dim DocCount As Integer
DocCount = CATIA.Documents.Count

Dim NameArray(3) As String
    NameArray(0) = CATIA.Documents.Item(1).Name
    NameArray(1) = CATIA.Documents.Item(2).Name
    NameArray(2) = CATIA.Documents.Item(3).Name

If NameArray(0) <> Right(".CATPart", 8) Then
   MsgBox "There are no CATIA Files open.", vbInformation
    Exit Sub
  End If

I've also thought about getting rid of the array and just going with

Code:
Dim DocCount As Integer
DocCount = CATIA.Documents.Count

Dim n as integer
For n = 1 to DocCount step 1
If CATIA.Documents.Item(n).Name <> Right(".CATPart", 8) Then
   MsgBox "There are no CATIA Files open.", vbInformation
    Exit Sub
  End If

Next n
'... duplicate the above for .catprduct and .catdrawing

any thoughts/ideas??

 
Replies continue below

Recommended for you

but you just want to run your script if your ActiveDocument has the correct file extension (catpart, catdrawing, catproduct). and there is only one ActiveDocument in session... so, don't mind the other CATIA.Documents and check that the active one is of the right kind...

regards,
LWolf
 
Well, a better way to put it would be to say the only 3 document file extensions allowed are .CATDrawing, .CATPart, and .CATProduct. Those are the only 3 types my script is intended to interface with currently.
 
Code:
if CATIA.Windows.Count > 0 then
  If UBound(Filter(Array("DrawingDocument" ,"PartDocument", "ProductDocument"), TypeName(CATIA.ActiveDocument))) = -1 then
    Exit sub
  End if
End if
 
a few questions;

what is happening or what does adding the ": doctype=typename()" achieve? I haven't seen that used before.

Why the "-1" for If Ubound(...)Then? Would using <1 also work? I see you're going for a value less than 1, I'm just wondering why not use 0?

Thanks, I'll try the code later today.

 
first index is 0, so less than 0 should also work...
the colon concatinates two rows of the variable definition, it is also more readable. TypeName is used to get the type of the object (point, line, part, product...)
there is a little space in the if ... then statement, should be CATIA.ActiveDocument, but I'm sure you'd catch that yourself.

regards,
LWolf
 
Ran the code, here are the results.

Code:
'test if background files present
'Dim DocName As String
'DocName = CATIA.Documents.Item(1).Name

'MsgBox (DocName)

If CATIA.Windows.Count > 0 Then
Dim DocType: DocType = TypeName(CATIA.ActiveDocument)
If UBound(Filter(Array("DrawingDocument", "PartDocument", "ProductDocument"), TypeName(CATIA.ActiveDocument))) = -1 Then
MsgBox ("Message to user***change me***."), vbInformation
Exit Sub
End If
End If

works if files are open, fails if no files are open due to the windows count portion set as "> 0"

So, rather than add more code, I'm electing to keep it simple and remove the filter section and just use "...windows.count < 1". Works fine for my purposes.
 
Status
Not open for further replies.
Back
Top