Part Name Extraction for DXF Macro

Discussion in 'SolidWorks' started by inthepickle, Mar 16, 2006.

  1. inthepickle

    inthepickle Guest

    I need some help in a bad way. I have a macro that will save a part as
    a DXF if certain conditions are true. My problem is the way that this
    macro sets the file name. I have to save my part in a certain folder
    in order this macro to work which is not the way I need it to work. I
    need a way to take this parth

    D:\Storage\DBWorks\TestPart.SLDPRT

    and extract this

    TestPart.SLDPRT - or just this - Testpart.

    I need to be able to do this automatically, no matter what directory
    the part is located in.

    Will one you you guys help me out. I am no expert by any strech of the
    imaginatyion. My macro is posted below. It will not work in your
    version of solidworks because you do not have the add-in, I even
    covered up my library name so as to not revela too much,but I am sure
    someone will still be able to help me out.



    Declare Sub ExportDXFFile Lib "???????" (ByVal fname As String, ByVal
    tname As String)

    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Sub main()

    Dim FileName As String
    Dim FileNameNoExt As String
    Dim DrwPathName As String
    Dim DxfPathName As String
    Dim Nested As String

    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc

    ModelPathName = swModel.GetPathName

    'parts can only be stored in this location -
    D:\Storage\DBWorks\TestPart.SLDPRT
    'the -19 removes the file path and just leaves the file name
    'will not work in another directory unless the -19 is changed
    FileName = Right(ModelPathName, Len(ModelPathName) - 19)

    'takes the file name TestPart.SLDPRT and removes the extension and
    'just leaves - TestPart.
    FileNameNoExt = Left(FileName, Len(FileName) - 6)

    'establishes the path and the file name for the dxf
    DxfPathName = "C:\Documents and Settings\DHales\Desktop\" +
    FileNameNoExt + "dxf"

    'exportdxffile is part of the library that i have as an add-in
    'this is the location of the drawing template used to insert the
    flat pattern of the part 1:1
    'and then save the dxf
    ExportDXFFile DxfPathName,
    "Y:\DBWORKS_SERVER\PAR\FlatPattern_FullScale.DRWDOT"

    End Sub
     
    inthepickle, Mar 16, 2006
    #1
  2. inthepickle

    TOP Guest

    There is a reverse InStr function that can work nicely for parseing
    filenames.
     
    TOP, Mar 16, 2006
    #2
  3. inthepickle

    inthepickle Guest

    can you give me the specific code to extract a file name from a path?
     
    inthepickle, Mar 16, 2006
    #3
  4. inthepickle

    TOP Guest

    Look up InStr in the API help for VBA. Then look at related items.
    Search for the \ and then the .. It is pretty simple once you find the
    function.
     
    TOP, Mar 16, 2006
    #4
  5. -------------------------------------------
    Function GetActiveDocName()
    Dim FilePathName As String
    FilePathName = CurrentDoc.GetPathName ' This is the whole string-including
    the path
    If Not FilePathName = "" Then GetActiveDocName = StripName(FilePathName)
    End Function
    -------------------------------------------
    'Recursive function to strip the file name from the path of the current
    part.
    'INPUT: string, the file name including path
    'RETURN: string, file name only
    Function StripName(FName As String) As String
    If Not (Right(FName, 1) = "\") Then
    StripName = StripName(Left(FName, Len(FName) - 1)) & Right(FName, 1)
    Else
    FilePath = Left(FName, Len(FName) - 1) ' Grab the full path while we
    have it separated
    StripName = ""
    End If
    End Function
    -------------------------------------------
    WT


    *** Free account sponsored by SecureIX.com ***
    *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
     
    Wayne Tiffany, Mar 16, 2006
    #5
  6. inthepickle

    Fye Guest

    Here's another way...

    PathAndFile = "D:\Storage\DBWorks\TestPart.SLDPRT"
    '(you set this somehow)

    If Dir(PathAndFile) <> "" Then
    JustTheFileName = Dir(PathAndFile )
    PathAndFileNameWithNoExtension = Left(PathAndFile , (Len(PathAndFile
    ) - 7))
    JustThePath = Left(PathAndFile , (Len(PathAndFile ) -
    len(JustTheFileName)))
    EndIf



    In your example, here are the results:
    JustTheFileName = "TestPart.SLDPRT"
    PathAndFileNameWithNoExtension = "D:\Storage\DBWorks\TestPart"
    JustThePath = "D:\Storage\DBWorks\TestPart"
     
    Fye, Mar 17, 2006
    #6
  7. inthepickle

    Heikki Leivo Guest

    StripName = StripName(Left(FName, Len(FName) - 1)) & Right(FName, 1)

    Haha :-D OK, recursion works of course but IMO it is like killing flies with
    a sledgehammer in this case. How abot this

    Dim fullPath As String
    fullPath = model..GetPathName

    Dim slashPosition As Integer
    slashPosition = InStrRev(fullPath, "\") 'Gets the position of last \

    Dim fileName as String
    fileName = Right(fullPath, Len(fullPath) - slashPosition)

    -h-
     
    Heikki Leivo, Mar 17, 2006
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.