AnsweredAssumed Answered

Can you help with Left() function not working properly on all machines?

Question asked by Andy Casper on Jun 28, 2019
Latest reply on Jun 29, 2019 by Deepak Gupta

Hi all, I am having an issue with a Macro we are using to automatically generate drawings for all of our configured parts.  It works great on my machine and a few others, but on some machines my variable sOutputFolder does not get the correct string.  I went as far as using MsgBox to display the swModel.GetPathName() and swModel.GetTitle(), but the subtraction of the length does seem to work properly.  My line Left(swModel.GetPathName(), Len(swModel.GetPathName()) - Len(swModel.GetTitle())) resolves the path of the file and stops at the "/" on my computer and a few others, but some are only removing part of the swModel.GetTitle().  I have included my code below.  I think it might have something to do with how Windows is looking at the files or folder locations.  Has anyone else run into this?  Code is below.  Thank you for the help

 

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim vConfs As Variant
Dim i As Integer
Dim sDrTemplate As String
Dim lDrSize As Long
Dim FieldName As String
Dim boolstatus As Boolean
Dim ResolvedValOut As String
Dim sOutputFolder As String

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Dim swPart As SldWorks.PartDoc
Dim currentSheet As Sheet
Dim spaceSide As Double
Dim spaceBetweenViews As Double
spaceSide = 0.1
spaceBetweenViews = 0.15

sOutputFolder = Left(swModel.GetPathName(), Len(swModel.GetPathName()) - Len(swModel.GetTitle()))
sDrTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateDrawing)
lDrSize = swDwgPaperSizes_e.swDwgPaperDsize
FieldName = "Part Number"

vConfs = swModel.GetConfigurationNames()

For i = 0 To UBound(vConfs)

Dim minX As Double
Dim minY As Double
Dim minZ As Double
Dim maxX As Double
Dim maxY As Double
Dim maxZ As Double
Dim x As Double
Dim y As Double
Dim z As Double
Dim j As Integer
Dim swBody As SldWorks.Body2
Dim vBodies As Variant

swModel.ShowConfiguration2 vConfs(i)
Set swPart = swModel
vBodies = swPart.GetBodies2(swBodyType_e.swSolidBody, True)
If Not IsEmpty(vBodies) Then
For j = 0 To UBound(vBodies)
Set swBody = vBodies(j)

swBody.GetExtremePoint 1, 0, 0, x, y, z
If j = 0 Or x > maxX Then maxX = x

swBody.GetExtremePoint -1, 0, 0, x, y, z
If j = 0 Or x < minX Then minX = x

swBody.GetExtremePoint 0, 1, 0, x, y, z
If j = 0 Or y > maxY Then maxY = y

swBody.GetExtremePoint 0, -1, 0, x, y, z
If j = 0 Or y < minY Then minY = y

swBody.GetExtremePoint 0, 0, 1, x, y, z
If j = 0 Or z > maxZ Then maxZ = z

swBody.GetExtremePoint 0, 0, -1, x, y, z
If j = 0 Or z < minZ Then minZ = z
Next
End If

Debug.Print "total views width: " & maxX - minX + maxZ - minZ
Debug.Print "total views height: " & maxY - minY + maxZ - minZ

Set swDraw = swApp.NewDocument(sDrTemplate, lDrSize, 0, 0)
Set currentSheet = swDraw.GetCurrentSheet

currentSheet.GetSize x, y
Debug.Print "width available for views: " & x - 2 * spaceSide - spaceBetweenViews
Debug.Print "height available for views: " & y - 2 * spaceSide - spaceBetweenViews

If (1 / 4 * (maxX - minX + maxZ - minZ) < x - 2 * spaceSide - spaceBetweenViews) And (1 / 4 * (maxX - minX + maxY - minY) < y - 2 * spaceSide - spaceBetweenViews) Then
boolstatus = currentSheet.SetScale(4, 1, True, False)

ElseIf (1 / 2 * (maxX - minX + maxZ - minZ) < x - 2 * spaceSide - spaceBetweenViews) And (1 / 2 * (maxX - minX + maxY - minY) < y - 2 * spaceSide - spaceBetweenViews) Then
boolstatus = currentSheet.SetScale(2, 1, True, False)

ElseIf (1 * (maxX - minX + maxZ - minZ) < x - 2 * spaceSide - spaceBetweenViews) And (1 * (maxX - minX + maxY - minY) < y - 2 * spaceSide - spaceBetweenViews) Then
boolstatus = currentSheet.SetScale(1, 1, True, False)

ElseIf 2 * (maxX - minX + maxZ - minZ) < x - 2 * spaceSide - spaceBetweenViews And (2 * (maxX - minX + maxY - minY) < y - 2 * spaceSide - spaceBetweenViews) Then
boolstatus = currentSheet.SetScale(1, 2, True, False)

ElseIf 4 * (maxX - minX + maxZ - minZ) < x - 2 * spaceSide - spaceBetweenViews And (4 * (maxX - minX + maxY - minY) < y - 2 * spaceSide - spaceBetweenViews) Then
boolstatus = currentSheet.SetScale(1, 4, True, False)

End If

swDraw.Create3rdAngleViews2 swModel.GetPathName

'Get "Part Number" variable from data card for active configuration
ResolvedValOut = swModel.GetCustomInfoValue(vConfs(i), FieldName)

Dim swDrawModel As SldWorks.ModelDoc2
Set swDrawModel = swDraw

swDrawModel.ForceRebuild3 True

'swInsertAnnotation_e Enumeration
'http://help.solidworks.com/2017/english/api/swconst/SOLIDWORKS.Interop.swconst~SOLIDWORKS.Interop.swconst.swInsertAnnotation_e.html

'Insert Dimensions Marked for Drawing
swDraw.InsertModelAnnotations3 swImportModelItemsSource_e.swImportModelItemsFromEntireModel, 32776, True, True, False, False

'Insert Hole Callout
swDraw.InsertModelAnnotations3 swImportModelItemsSource_e.swImportModelItemsFromEntireModel, 1048576, True, True, False, False

'Insert Instance Counts
'swDraw.InsertModelAnnotations3 swImportModelItemsSource_e.swImportModelItemsFromEntireModel, 16, True, True, False, False

'Insert Notes
swDraw.InsertModelAnnotations3 swImportModelItemsSource_e.swImportModelItemsFromEntireModel, 64, True, True, False, False

'Insert Surfaces
swDraw.InsertModelAnnotations3 swImportModelItemsSource_e.swImportModelItemsFromEntireModel, 4096, True, True, False, False

'Insert Cosmetic Threads
swDraw.InsertModelAnnotations3 swImportModelItemsSource_e.swImportModelItemsFromEntireModel, 1, True, True, False, False


swDrawModel.Extension.SaveAs sOutputFolder + ResolvedValOut + ".slddrw", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, 0, 0

swDrawModel.ForceRebuild3 True
'Second Save to pull data card information from model
swDrawModel.Save

swApp.CloseDoc swDrawModel.GetTitle()
Next
End Sub

Outcomes