Merge 2 codes

Merge 2 codes

madstrolle
Enthusiast Enthusiast
442 Views
4 Replies
Message 1 of 5

Merge 2 codes

madstrolle
Enthusiast
Enthusiast

Hi Guys,

I have this code (under picture), I got from @FINET_Laurent , which creates PDF and DXF of my drawings and place them in the right folders. It's working fine.

But I would like to have the sheet metal parts created from the "save copy as", as in the picture below. 

I found a code that could do exactly this, (last code).

I would like to have these codes merged somehow, so when I use the rule it shall generate PDF and DXF as it already does, but if there's sheet metal parts in the drawing, then they should be generated as shown in the picture. And place them in the correct folders, like in the first code.

I hope it makes sence 😊

 

madstrolle_1-1697205707415.png

 

Dim actDoc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim file As String = actDoc.FullFileName 'full drawing path
Dim name As String = actDoc.DisplayName 'drawing name

Dim dir As String = "03. Native"
Dim targetDir As String = "01. PDF"

Dim s As String = Left(file, file.Length - (dir.Length + (name.Length + 4)) -1) '-1 for \
Dim pdfname As String = name & ".pdf" 

Dim pdfdoc As String = s & targetDir & "\" & pdfname

ThisDoc.Launch(pdfdoc)

 

Dim oDoc As Document = ThisApplication.ActiveDocument 'Get active document
If oDoc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub 'Drawings only
For Each oModel As Document In oDoc.ReferencedDocuments 'Loop through all referenced documents
'Sheet metal parts only
If oModel.DocumentSubType.DocumentSubTypeID <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
Dim oSMCD As SheetMetalComponentDefinition = oModel.ComponentDefinition
If Not oSMCD.HasFlatPattern Then 'If it doesn't have a flat patter, create one (unfold the model)
oSMCD.Unfold()
oSMCD.FlatPattern.ExitEdit()
End If
Dim FName As String = oModel.FullFileName 'Get the document's path and name
FName = Microsoft.VisualBasic.Left(FName, Len(FName) - 4) & ".dxf"
Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=R12" 'Export settings
Try
oSMCD.DataIO.WriteDataToFile(sOut, FName) 'Export
Catch
End Try
Next

0 Likes
Accepted solutions (1)
443 Views
4 Replies
Replies (4)
Message 2 of 5

A.Acheson
Mentor
Mentor

Hi @madstrolle 

 

Doesn't look like a difficult task. Where would you like help? You will need to study each line and find the corresponding object and see where to insert. Can you attach the attempted merged code? Attach any error messages you receive. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 5

madstrolle
Enthusiast
Enthusiast

Hi @A.Acheson ,

 

Thank you for your quick answer.

I have tried different combos, but nothing seems to work as intended.

I'm not a programmer, so i'm just trying stuff.

So i hoped that someone could help me and put the code correct together.

0 Likes
Message 4 of 5

A.Acheson
Mentor
Mentor

Hi @madstrolle 

It is good that your trying the attempts. Can you attach the attempts here. Much easier to make a few changes to a code than to build it from scratch. It also good to have your attempts reviewed so you can learn where you might have gone wrong.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 5

madstrolle
Enthusiast
Enthusiast
Accepted solution

Hi @A.Acheson ,

 

I's working now😊

 

Here is what I ended up with:

 

Dim oDoc As Document = ThisApplication.ActiveDocument

If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
Dim file_path As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)

If Right(file_path, 10) = "03. Native" Then
Dim new_file_path As String = Left(file_path, Len(file_path) - 10)

' Create the "02. DXF" folder in the same directory as the drawing
Dim dxf_folder_path As String = System.IO.Path.Combine(new_file_path, "02. DXF")
System.IO.Directory.CreateDirectory(dxf_folder_path)

' Create the "01. PDF" folder in the same directory as the drawing
Dim pdf_folder_path As String = System.IO.Path.Combine(new_file_path, "01. PDF")
System.IO.Directory.CreateDirectory(pdf_folder_path)

' Loop through all drawing views
Dim drawingSheet As Sheet = oDoc.ActiveSheet
For Each oDrawingView As DrawingView In drawingSheet.DrawingViews
If oDrawingView.ReferencedFile IsNot Nothing Then
Dim oPartDoc As PartDocument = TryCast(oDrawingView.ReferencedFile.ReferencedDocument, PartDocument)
If oPartDoc IsNot Nothing Then
Dim oSMCD As SheetMetalComponentDefinition = oPartDoc.ComponentDefinition
If Not oSMCD.HasFlatPattern Then
oSMCD.Unfold()
oSMCD.FlatPattern.ExitEdit()
End If
Dim part_file_name As String = System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName)
Dim dxf_file_path As String = System.IO.Path.Combine(dxf_folder_path, part_file_name & ".dxf")
Dim sOut As String = "FLAT PATTERN DXF?AcadVersion=R12"
Try
oSMCD.DataIO.WriteDataToFile(sOut, dxf_file_path)
Catch ex As Exception
MsgBox("Error exporting DXF: " & ex.Message, vbExclamation, "DXF Export Error")
End Try
End If
End If
Next

' Generate the PDF file using your provided code
Dim file_path_length As Integer = Len(file_path)
Dim file_name As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
Dim pdf_file_path As String = System.IO.Path.Combine(pdf_folder_path, file_name & ".pdf")

' Use your provided PDF generation code here
file_name_path_pdf = new_file_path & "\" & "01. PDF" & "\" & file_name
oDoc.SaveAs(pdf_file_path, True)

Else
MsgBox("Forkert filsti. Der vil ikke blive lavet PDF og DXF filer.", vbExclamation, "Lav PDF/DXF")
End If
End If

0 Likes