Open drawing - Either DWG or IDW based on Part number

Open drawing - Either DWG or IDW based on Part number

alexander.duell
Contributor Contributor
488 Views
5 Replies
Message 1 of 6

Open drawing - Either DWG or IDW based on Part number

alexander.duell
Contributor
Contributor

Hello,

 

I have this Ilogic code, see below:

 

im oOccurrence As ComponentOccurrence
Dim Prop As String

Try
  oOccurrence = ThisDoc.Document.SelectSet.Item(1)
Catch
  Ritnummer = iProperties.Value("Project", "Part Number")

				ThisDoc.Launch(Ritnummer + ".idw")
				
  Return
End Try

If TypeOf oOccurrence Is ComponentOccurrenceProxy Then
    Prop = iProperties.Value(oOccurrence.NativeObject.Name, "Project", "Part Number")
Else 
    Prop = iProperties.Value(oOccurrence.Name, "Project", "Part Number")
End If
'Display Property in Msg Box

Ritnummer = iProperties.Value(oOccurrence.Name, "Project", "Part Number")

Try 'Try open a drawing with the same name as this model
				ThisDoc.Launch(Ritnummer + ".idw")
			Catch 'If no model is found show a message box
				MessageBox.Show("Hittar ej någon ritning för markerad del" & vbCrLf & "Ritningsnummer: " & iProperties.Value(oOccurrence.Name, "Project", "Description")& vbCrLf &"Modellnamn: "  & Ritnummer , "FEL")

		End Try
	

 For now, it only works for IDW's.

 

I would like to mod it to work also for DWG's.  Basically, a loop that tries to open a dwg, if that fails, try to open aidw, if that also fails, give the messagebox that tells that there isnt a drawing.

 

 

How would that Ilogic look like? Im having a hard time figuring that loop out.

 

Best Regards
Alex

0 Likes
489 Views
5 Replies
Replies (5)
Message 2 of 6

FINET_Laurent
Advisor
Advisor

Hi @alexander.duell,

 

You could simply add a try/catch statement in the existing catch statement as folowing :

Dim oOccurrence As ComponentOccurrence
Dim Prop As String

Try
 oOccurrence = ThisDoc.Document.SelectSet.Item(1)
 
Catch
	 Ritnummer = iProperties.Value("Project", "Part Number")
	ThisDoc.Launch(Ritnummer + ".idw")				
	Return
	
End Try

If TypeOf oOccurrence Is ComponentOccurrenceProxy Then
	Prop = iProperties.Value(oOccurrence.NativeObject.Name, "Project", "Part Number")
	
Else 
	Prop = iProperties.Value(oOccurrence.Name, "Project", "Part Number")
	
End If
'Display Property in Msg Box

Ritnummer = iProperties.Value(oOccurrence.Name, "Project", "Part Number")

Try 'Try open a drawing with the same name as this model
	ThisDoc.Launch(Ritnummer + ".idw")
	
Catch 'If no model is found show a message box
	Try 
		ThisDoc.Launch(Ritnummer + ".dwg")
		
	Catch
		MessageBox.Show("Hittar ej någon ritning för markerad del" & vbCrLf & "Ritningsnummer: " & iProperties.Value(oOccurrence.Name, "Project", "Description") & vbCrLf & "Modellnamn: " & Ritnummer, "FEL")
	
	End Try
End Try

 

But I think it would be better to check if the file exists before trying to open it.. thus avoiding to intentionally throw an error. You can check if a file exists like so :

If IO.File.Exists(Ritnummer + ".idw") = True Then 
	ThisDoc.Launch(Ritnummer + ".idw")
	Return

End If

If IO.File.Exists(Ritnummer + ".dwg") = True Then 
	ThisDoc.Launch(Ritnummer + ".dwg")
	Return

End If

MessageBox.Show("Hittar ej någon ritning för markerad del" & vbCrLf & "Ritningsnummer: " & iProperties.Value(oOccurrence.Name, "Project", "Description")& vbCrLf &"Modellnamn: "  & Ritnummer , "FEL")

Does this suits your needs ? 

 

Kind regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 3 of 6

alexander.duell
Contributor
Contributor

Hello,

 

Yes i think the IF loop is hte way to go.

 

But how would i implement that in the code? 

 

I think the code as meant to work as if nothing is marked it tries to the the drawing of the part/ assembly that is open.

 

But when using part priority, it opens the drawing of the selected part.

 

Best Regards
Alex

 

 

0 Likes
Message 4 of 6

FINET_Laurent
Advisor
Advisor

@alexander.duell,

 

Can you provide us the full function so we can have a better understanding of what it is supposed to do ?

 

Kind regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 5 of 6

alexander.duell
Contributor
Contributor

Hello,

 

Oh, i thought i did.

 

Well, se below:

 

Dim oOccurrence As ComponentOccurrence
Dim Prop As String

Try
  oOccurrence = ThisDoc.Document.SelectSet.Item(1)
Catch
  Ritnummer = iProperties.Value("Project", "Part Number")

				ThisDoc.Launch(Ritnummer + ".idw")
				
  Return
End Try

If TypeOf oOccurrence Is ComponentOccurrenceProxy Then
    Prop = iProperties.Value(oOccurrence.NativeObject.Name, "Project", "Part Number")
Else 
    Prop = iProperties.Value(oOccurrence.Name, "Project", "Part Number")
End If
'Display Property in Msg Box

Ritnummer = iProperties.Value(oOccurrence.Name, "Project", "Part Number")

Try 'Try open a drawing with the same name as this model
				ThisDoc.Launch(Ritnummer + ".idw")
			Catch 'If no model is found show a message box
				MessageBox.Show("Hittar ej någon ritning för markerad del" & vbCrLf & "Ritningsnummer: " & iProperties.Value(oOccurrence.Name, "Project", "Description")& vbCrLf &"Modellnamn: "  & Ritnummer , "FEL")

		End Try
		

 Best Regards

Alex

0 Likes
Message 6 of 6

FINET_Laurent
Advisor
Advisor

Hi @alexander.duell

 

No you didn't. I'm asking because I see you are using a return statement in your code, but not returning any value.. Anyway, here is the full function with the code implemented :

Private Function openDoc() As Boolean
	Dim oOccurrence As ComponentOccurrence
	Dim Prop As String
 
	Try
		oOccurrence = ThisDoc.Document.SelectSet.Item(1)
	 
	Catch
		Ritnummer = iProperties.Value("Project", "Part Number")
		ThisDoc.Launch(Ritnummer + ".idw")				
		Return True
		
	End Try

	If TypeOf oOccurrence Is ComponentOccurrenceProxy Then
		Prop = iProperties.Value(oOccurrence.NativeObject.Name, "Project", "Part Number")
		
	Else 
		Prop = iProperties.Value(oOccurrence.Name, "Project", "Part Number")
		
	End If
	
	Ritnummer = iProperties.Value(oOccurrence.Name, "Project", "Part Number")

	If IO.File.Exists(Ritnummer + ".idw") = True Then 
		ThisDoc.Launch(Ritnummer + ".idw")
		Return True

	End If

	If IO.File.Exists(Ritnummer + ".dwg") = True Then 
		ThisDoc.Launch(Ritnummer + ".dwg")
		Return True

	End If

	MessageBox.Show("Hittar ej någon ritning för markerad del" & vbCrLf & "Ritningsnummer: " & iProperties.Value(oOccurrence.Name, "Project", "Description")& vbCrLf &"Modellnamn: "  & Ritnummer , "FEL")
	Return False
	
End Function

 

Here is also some lecutre that might help you understand :

Return Statement - Visual Basic | Microsoft Learn

Procedures - Visual Basic | Microsoft Learn

 

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes