Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Is it to hard to do? 1

Status
Not open for further replies.

RBasniak

Mechanical
Sep 19, 2001
59
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
 
Replies continue below

Recommended for you

You plan on using a map file when producing this files?

Thats my first question.

If you don't know what a map file is look in the help real quick.

Regards, Scott Baugh, CSWP [spin] [americanflag]
3DVision Technologies
credence69@REMOVEhotmail.com

*When in doubt always check the help*
 
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....
 
Not really!
Code:
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 <> &quot;&quot;
        'Open Drawing - Define Titles
        Set swPart = swApp.OpenDoc(sPath & sFileSW, swDocDrawing)
        Set swDwg = swPart
        sFileDWG = Left(sFileSW, Len(sFileSW) - 6) & &quot;DWG&quot;
        sFileDXF = Left(sFileSW, Len(sFileSW) - 6) & &quot;DXF&quot;
        sFileTitle = swPart.GetTitle
        'Save Sheet 1 (If Present)
        If swDwg.ActivateSheet(&quot;Sheet1&quot;) Then
            swPart.SaveAs2 sPath & sFileDWG, 0, True, False
        Else
            swApp.SendMsgToUser &quot;No Sheet1 in &quot; & sFileSW
        End If
        'Save Sheet 2
        If swDwg.ActivateSheet(&quot;Sheet2&quot;) Then
            swPart.SaveAs2 sPath & sFileDXF, 0, True, False
        Else
            swApp.SendMsgToUser &quot;No Sheet2 in &quot; & sFileSW
        End If
        'Close File
        swApp.CloseDoc sFileTitle
        'Next File
        sFileSW = Dir
    Loop
    swApp.SendMsgToUser &quot;Done!&quot;
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.
 
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
 
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
&quot;Dim swXXX as Object&quot; 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.
 
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 :-D I must learn macros as soon as I can LOL

Just two questions about it:

- can macros print into a PRN file?

- in this macro, there's this line that should close the file: &quot;swApp.CloseDoc sFileTitle&quot;, 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
 
- 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.
 
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 :-D

[]'s
Rodrigo Basniak
 
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.
Code:
'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.
 
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.

Code:
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 = &quot;C:\Temp\&quot;
    
    Set swApp = CreateObject(&quot;SldWorks.Application&quot;)
    
    sFileSW = Dir(sPath & &quot;*.slddrw&quot;)
    Do While sFileSW <> &quot;&quot;
        'Open Drawing - Define Titles
        Set swPart = swApp.OpenDoc(sPath & sFileSW, swDocDrawing)
        Set swDwg = swPart
        
        sFileDWG = Left(sFileSW, Len(sFileSW) - 6) & &quot;DWG&quot;
        sFileDXF = Left(sFileSW, Len(sFileSW) - 6) & &quot;DXF&quot;
        sFilePRN = Left(sFileSW, Len(sFileSW) - 6) & &quot;PRN&quot;
        'Save Sheet 1 (If Present)
        If swDwg.ActivateSheet(&quot;Sheet1&quot;) Then
            'Save as DWG
            swPart.SaveAs2 sPath & sFileDWG, 0, True, False
            'Print to PRN
            swPart.PrintOut2 1, 1, 1, False, &quot;&quot;, 1, True, sPath & sFilePRN
        Else
            swApp.SendMsgToUser &quot;No Sheet1 in &quot; & sFileSW
        End If
        'Save Sheet 2
        If swDwg.ActivateSheet(&quot;Sheet2&quot;) Then
            swPart.SaveAs2 sPath & sFileDXF, 0, True, False
        Else
            swApp.SendMsgToUser &quot;No Sheet2 in &quot; & sFileSW
        End If
        'Close File
        sFileTitle = swPart.GetTitle
        swApp.CloseDoc sFileTitle
        'Next File
        sFileSW = Dir
    Loop
    swApp.SendMsgToUser &quot;Done!&quot;
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.
 
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:
Code:
sPath = &quot;C:\Temp\&quot;
with
Code:
Dim sMsg As String
sMsg = &quot;Enter the Path to the Drawings.&quot; & vbCrLf * _
       &quot;Make sure to end it with a \&quot;
sPath = InputBox(sMsg, &quot;Enter Drawing Path&quot;, &quot;C:\Temp\&quot;)
If sPath = &quot;&quot; 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.
 
The problem when closing files was resolved :-D

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
 
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.
 
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
 
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.
 
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?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor