×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Is it to hard to do?

Is it to hard to do?

Is it to hard to do?

(OP)
Hello people...

Let's see if anyone can help me... is it too hard to create a macro that to the following:

- Open a drawing file
- Save as Sheet 1 as dwg file
- Save as Sheet 2 as dxf file
- Close the file
- Open the next file and then so with all other drwaings into the folder.

Is it possible?

Thank you so much and Merry Xtmas for all! :)

Rodrigo Basniak

RE: Is it to hard to do?

Interesting thread, if you get any results from it, can I have the product?

related to saving to autocad format...

I have had problems saving piping assembly drawings to dwg and dxf, additional piping centerlines gets into the drawing, offset from the pipes. No fun, have to delete them manually....

RE: Is it to hard to do?

Not really!

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swPart As ModelDoc2
Dim swDwg As DrawingDoc

Const swDocDrawing = 3

Sub main()
    Dim sPath As String
    Dim sFileSW As String
    Dim sFileDWG As String
    Dim sFileDXF As String
    Dim sFileTitle As String
    
    sPath = "C:\Temp\"
    
    Set swApp = CreateObject("SldWorks.Application")
    
    sFileSW = Dir(sPath & "*.slddrw")
    Do While sFileSW <> ""
        'Open Drawing - Define Titles
        Set swPart = swApp.OpenDoc(sPath & sFileSW, swDocDrawing)
        Set swDwg = swPart
        sFileDWG = Left(sFileSW, Len(sFileSW) - 6) & "DWG"
        sFileDXF = Left(sFileSW, Len(sFileSW) - 6) & "DXF"
        sFileTitle = swPart.GetTitle
        'Save Sheet 1 (If Present)
        If swDwg.ActivateSheet("Sheet1") Then
            swPart.SaveAs2 sPath & sFileDWG, 0, True, False
        Else
            swApp.SendMsgToUser "No Sheet1 in " & sFileSW
        End If
        'Save Sheet 2
        If swDwg.ActivateSheet("Sheet2") Then
            swPart.SaveAs2 sPath & sFileDXF, 0, True, False
        Else
            swApp.SendMsgToUser "No Sheet2 in " & sFileSW
        End If
        'Close File
        swApp.CloseDoc sFileTitle
        'Next File
        sFileSW = Dir
    Loop
    swApp.SendMsgToUser "Done!"
End Sub

If you use this code, you must add the SldWrks200? Type Library to your Project > References. Or, you can simple late bind the SolidWorks objects:
Dim swApp As Object
Dim swPart As Object
Dim swDwg As Object

This will go through all SolidWorks Drawings in the folder specified in the definition of sPath.

Hope it helps...

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Is it to hard to do?

(OP)
Hi DSI,

Sorry, but I don't know anything about macros (yet) So I didn't understand what did you mean in final of the message. Tell what I missed: I copied the code, pasted into a new macro, then opened SW and tried to run the macro. But nothing happend... of course because I didn't did nothing with this:

Dim swApp As Object
Dim swPart As Object
Dim swDwg As Object

Where should I put it?

Thanks for the attention,
Rodrigo Basniak

RE: Is it to hard to do?

RBasniak:

With your macro open, go to Tools > References.
Scroll down until you find the SldWrks 200? Type Library
and check it. You will not have to do anything with the
"Dim swXXX as Object" statements.

Then, make sure to change sPath so that it points to the directory that contains the drawings you with to convert.

Let me know if this clears things up.

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Is it to hard to do?

(OP)
Hello DSI,

What T-E-R-R-I-F-I-C results!!!!! I don't have idea of how much work you saved for me in every new project I must learn macros as soon as I can

Just two questions about it:

- can macros print into a PRN file?

- in this macro, there's this line that should close the file: "swApp.CloseDoc sFileTitle", right? At the end, all files are still opened. This is correct? If yes, don't worry, it's not a problem if I have to close them at the end.

[]'s
Rodrigo Basniak

RE: Is it to hard to do?

- Yes, you should be able to print to a file. Do you want to print every sheet that is being exported? Let me know. It should be easy enough to add to the code above.

- Yes, the macro should close each file by name. It works on my system, so I am not sure why it wouldn't on yours.

Glad I could help...

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Is it to hard to do?

(OP)
DSI,

- Sheet1 is always the Detail drawing, sheet2 is always a file that goes to the laser cutting machine, there's no need to print this one. Only sheet1 must be printed.

Don't worry about closing files it's not a big problem

[]'s
Rodrigo Basniak

RE: Is it to hard to do?

My bad! I do know why they are not closing. Move the line for sTitle right before the close statement. When the sheets get switched, the name changes.

'Close File
sFileTitle = swPart.GetTitle
swApp.CloseDoc sFileTitle


I will work on the printing portion and repost the corrected code.

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Is it to hard to do?

OK, this includes the printing of Sheet1 to a .prn file. I simply printed using a scale of 1. If you want to use the inverse of the drawing scale, let me know. It's easy enough to change.


Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swPart As ModelDoc2
Dim swDwg As DrawingDoc

Const swDocDrawing = 3

Sub main()
    Dim sPath As String
    Dim sFileSW As String
    Dim sFileDWG As String
    Dim sFileDXF As String
    Dim sFilePRN As String
    Dim sFileTitle As String
    
    sPath = "C:\Temp\"
    
    Set swApp = CreateObject("SldWorks.Application")
    
    sFileSW = Dir(sPath & "*.slddrw")
    Do While sFileSW <> ""
        'Open Drawing - Define Titles
        Set swPart = swApp.OpenDoc(sPath & sFileSW, swDocDrawing)
        Set swDwg = swPart
        
        sFileDWG = Left(sFileSW, Len(sFileSW) - 6) & "DWG"
        sFileDXF = Left(sFileSW, Len(sFileSW) - 6) & "DXF"
        sFilePRN = Left(sFileSW, Len(sFileSW) - 6) & "PRN"
        'Save Sheet 1 (If Present)
        If swDwg.ActivateSheet("Sheet1") Then
            'Save as DWG
            swPart.SaveAs2 sPath & sFileDWG, 0, True, False
            'Print to PRN
            swPart.PrintOut2 1, 1, 1, False, "", 1, True, sPath & sFilePRN
        Else
            swApp.SendMsgToUser "No Sheet1 in " & sFileSW
        End If
        'Save Sheet 2
        If swDwg.ActivateSheet("Sheet2") Then
            swPart.SaveAs2 sPath & sFileDXF, 0, True, False
        Else
            swApp.SendMsgToUser "No Sheet2 in " & sFileSW
        End If
        'Close File
        sFileTitle = swPart.GetTitle
        swApp.CloseDoc sFileTitle
        'Next File
        sFileSW = Dir
    Loop
    swApp.SendMsgToUser "Done!"
End Sub


DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Is it to hard to do?

I thought of one more thing that would make it easier to use. Instead of changing the macro every time for the drawing path, you can make it an input.

Simply replace the one line:

sPath = "C:\Temp\"

with

Dim sMsg As String
sMsg = "Enter the Path to the Drawings." & vbCrLf * _
       "Make sure to end it with a \"
sPath = InputBox(sMsg, "Enter Drawing Path", "C:\Temp\")
If sPath = "" Then Exit Sub

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Is it to hard to do?

(OP)
The problem when closing files was resolved

I'd run the macro but it didn't create any .PRN file. Only the .DWG and .DXF files. Do you know why?
The scale of the drawings are 1:1 all of them are always with page setup correct, the macro only needs to print them into a .PRN file with the same name of the Drawing file.

[]'s
Rodrigo Basniak

RE: Is it to hard to do?

The PrintOut2 method creates DwgName.PRN in the same directory as the drawing files.

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Is it to hard to do?

(OP)
Really sorry about troubling you again. The PRN aren't being created. I tried to find why into the SW API Reference but everything seems to be correct. Do you have idea why?

Thanks....
Rodrigo Basniak

RE: Is it to hard to do?

I have no idea. It works for me. If I have time, I will check into it.

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Is it to hard to do?

I see you mentioned that sheet 2 is going to a Laser ... can I assume that you are using Flat patterns here?  

If so, you may want to carefully check your dxf: sometimes bend lines, tiny datum points, (and in 2003, the centermark holes for points)  come thru in the dxf.

I never had much success getting a perfectly 'clean' dxf using the MAP files, I ended up having to write code to manually extract the geometry from the View and code my own DXF exporter.. if you succeed in exporting a SW dxf file that comes thru clean, Id love to hear about it.

What app are you using to code for the laser?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources