Okay, I've browsed and stolen from these forums long enough. I have hit a wall on this one and I can't find a work around, so any help on this would be greatly appreciated. I've written a lot of macros for use in the office and one of which converts our slddrw files to dxf's that are usable by our laser system. I've automated a lot of the process, but I have one hiccup I can't get passed on my latest upgrade: everytime I start the macro it will usually work the first time, but it will 100% fail the second time. Once the screen goes blank, there seens to be no way to recover other than to re-start solidworks. We usuallay have to convert hundreds of these drawings at a time, so it gets tedious.
Also, I don't consider myself a "programmer," I'm self-taught and my code is more Frankenstein in nature.
Again, any help on this would be appreciated.
Sub Slddrw()
Const FilRead = "C:\FabSubList.txt"
Dim myFil, strLine, strLen
Dim fs As Object
Dim myFolder
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swView As SldWorks.View
Dim swRootDrawComp As SldWorks.DrawingComponent
Dim vDrawChildCompArr As Variant
Dim vDrawChildComp As Variant
Dim swDrawComp As SldWorks.DrawingComponent
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim assemblyDrawing As String
Dim status As Boolean
Dim errors As Long
Dim warnings As Long
Dim lineWeight As Long
Dim lineThickness As Double
Set fs = CreateObject("Scripting.FileSystemObject")
myFil = FreeFile
Open FilRead For Input As #myFil
Do Until EOF(myFil)
Line Input #myFil, strLine
strLen = Len(strLine)
If strLen = 16 And strLine = ufFile.cbInput.Value Then
CurFil = ufFile.cbInput.Value
AliFil = ""
Exit Do
Else
If strLen = 32 And strLine = ufFile.cbInput.Value Then
CurFil = Left(ufFile.cbInput.Value, 16)
AliFil = Right(ufFile.cbInput.Value, 16)
Exit Do
End If
End If
Loop
Close #myFil
Dim fz As Object
Set fz = CreateObject("Scripting.FileSystemObject")
Dim Dest, Source1, Source2
Source1 = "G:\SolidWorks\Turning\" + CurFil + ".SLDDRW"
Source2 = "G:\SolidWorks\Machining\" + CurFil + ".SLDDRW"
Dest = CADdir + CurFil + ".SLDDRW"
If fz.FileExists(Dest) = False Then
If fz.FileExists(Source1) = True Then
fz.CopyFile Source1, CADdir
Else
If fz.FileExists(Source2) = True Then
fz.CopyFile Source2, CADdir
End If
End If
End If
Set swApp = Application.SldWorks
swApp.Visible = True
Set Part = swApp.OpenDoc6(Dest, 3, 1, "", longstatus, longwarnings)
'(ABOVE) 3 Opens drw files, 1 opens without errors (8 is rapiddraft)
Set Part = swApp.ActivateDoc2(CurFil & " - OpenDraw", False, longstatus)
swApp.ActiveDoc.ActiveView.FrameLeft = 0
swApp.ActiveDoc.ActiveView.FrameTop = 0
swApp.ActiveDoc.ActiveView.FrameState = 1
swApp.ActiveDoc.ActiveView.FrameState = 1
swApp.ActiveDoc.ActiveView.FrameState = 1
'On Error GoTo Out1
assemblyDrawing = Dest
Set swModel = swApp.OpenDoc6(assemblyDrawing, swDocDRAWING, swOpenDocOptions_Silent, "", errors, warnings)
Set swDraw = swModel
Set swModelDocExt = swModel.Extension
Set swSelMgr = swModel.SelectionManager
status = swDraw.ActivateView("Drawing View4")
status = swModelDocExt.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.104008832128, 0.1163870710783, 0, False, 0, Nothing, 0)
Set swView = swSelMgr.GetSelectedObject6(1, -1)
swModel.ViewZoomtofit2
Set swRootDrawComp = swView.RootDrawingComponent
'Debug.Print "File = " & swModel.GetPathName
'Debug.Print " View = " & swView.Name; vDrawChildCompArr = swRootDrawComp.GetChildren
layerer
Borderchange
Set Part = Nothing
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
Part.SaveAs2 CADdir + CurFil + ".DXF", 0, True, False
Out1:
Set Part = Nothing
swApp.CloseDoc CurFil + " - OpenDraw"
================ I attempted to separate the code to see if it would work better, but I'd prefer it to be all in one bit. ===============
Dim swApp As SldWorks.SldWorks
Dim filename As String
Dim boolstatus As Boolean
Dim longerrors As Long
Dim argString As String
Dim newDoc As Boolean 'SldWorks.ModelDoc2
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
filename = CADdir + CurFil + ".DXF"
'filename = "C:\Documents and Settings\dsims\Desktop\TEST\New Folder\4B1947C0370US010.DXF"
If fs.FileExists(filename) = False Then
MsgBox "File is not saved."
End If
Set swApp = Application.SldWorks
' Get the specified DXF/DWG import data
Dim importData As SldWorks.ImportDxfDwgData
Set importData = swApp.GetImportFileData(filename)
' To let SolidWorks determine an appropriate input file unit, do not set the LengthUnit property
'importData.LengthUnit("Model") = SwConst.swLengthUnit_e.swMETER
' To let SolidWorks determine an appropriate output paper size, do not set the PaperSize values
'boolstatus = importData.SetPaperSize("", SwConst.swDwgPaperSizes_e.swDwgPaperA4size, 0#, 0#)
' To let SolidWorks determine an appropriate sheet scale, do not set the SheetScale values.
boolstatus = importData.SetSheetScale("", SC1, SC2)
importData.DocumentTemplate = "C:\Program Files\SolidWorks\data\templates\FABBORDER.drwdot"
' Load the specified DXF/DWG file
Set newDoc = swApp.LoadFile4(filename, argString, importData, longerrors)
================================================
In pieces, this works great. If I step through, it ususally makes it through a few times, but it eventually fails and opens the DXF with a working tree on the left, but a gray screen.
I can select items, I just can't see what I'm doing. (See example.jpg)
Thanks,
Dan
SolidworksApi macros