AlexFielder / iLogic

The Unlicense
46 stars 16 forks source link

Rename Part Browser Names #7

Closed CrisDavis closed 3 years ago

CrisDavis commented 4 years ago

Alex,

Looking for an I-logic rule that renames browser names. I found the assembly rule but nothing for part files. Thanks for any assistance you can provide.

2020-05-26_125508

AlexFielder commented 4 years ago

Hi,

The approach is the same for Parts as is it for Assemblies; except the parent document is a PartDocument instead of the AssemblyDocument.

The following small method is from another repo on here (and relies upon System.Linq!):

Public Sub SaveCopyAsFromBrowserNodeNames()
        If TypeOf (ThisApplication.ActiveDocument) Is AssemblyDocument Then
            Dim searchstring As String = InputBox("what are we searching for?", "Search string", "Default Entry")

            If Not searchstring = String.Empty Then
                Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
                Dim oPane As BrowserPane = oDoc.BrowserPanes("Model")
                Dim oTopNode As BrowserNode = oPane.TopNode

                Dim nodelist As List(Of String) = New List(Of String)

                nodelist = (From a As BrowserNode In oTopNode.BrowserNodes
                            Let nodedef As BrowserNodeDefinition = a.BrowserNodeDefinition
                            Where nodedef.Label.Contains(searchstring)
                            Select nodedef.Label).ToList()
                ThisApplication.StatusBarText = searchstring

                '   For Each node As browsernode In oTopnode.Browsernodes
                '       Dim nodeDef as browsernodedefinition = node.browsernodedefinition
                '       If nodedef.label.startswith(searchstring) Then
                '           nodelist.add(nodedef.label)
                '           
                '       End If
                '       MessageBox.Show("browser node: " & nodedef.label)
                '   Next
                If nodelist.Count > 0 Then
                    Dim FolderName As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)
                    Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
                    For Each nodename As String In nodelist
                        Dim newfilename As String = FolderName & "\" & filename & "-" & nodename.Replace("°:1", "") & ".iam"
                        'newfilename.Replace("°:","°-")
                        MessageBox.Show(newfilename)
                        If Not System.IO.File.Exists(newfilename) Then
                            ThisApplication.ActiveDocument.SaveAs(newfilename, False)
                        End If
                    Next
                End If
            End If
        End If
    End Sub

As you can see, this targets an Assembly but it's fairly straightforward to rewrite to target part files.

If you can share what code you have so far I can offer some pointers on it.

CrisDavis commented 4 years ago

Thanks for your quick response.

This is what I have. Currently, it imports all my SAT files from c:\SAT into a single open part file. I would like for the browser names to be the file name of the SAT file.

At the end of the code I included something that changes the name of the solid bodies.

Option Explicit On Imports Directory = System.IO.Directory

Sub Main() ImportFilesInFolder("C:\SAT's") End Sub

Sub ImportFilesInFolder(folderName As String) Dim index = 0 For Each fileName As String In Directory.EnumerateFiles(folderName, "*.sat") Logger.Info("fileName = {0}", fileName) ImportSingleFile(fileName, index) index = index + 1 Next End Sub

Sub ImportSingleFile(fileName As String, index As Integer) ' Get the active part document Dim oDoc As PartDocument oDoc = ThisApplication.ActiveDocument

            ' Get transient Brep
            Dim oBrep As TransientBRep
            oBrep = ThisApplication.TransientBRep

            ' Get the surfacebodies
            Dim oSBs As SurfaceBodies
            oSBs = oBrep.ReadFromFile(fileName)

            ' To transform the body
            Dim oTG As TransientGeometry
            oTG = ThisApplication.TransientGeometry
            Dim oMatrix As Matrix
            oMatrix = oTG.CreateMatrix()

            ' Move the body arbitrarily
            'oMatrix.SetTranslation(oTG.CreateVector(index * 10, index * 10, 0))
            Dim oTransObjs As TransientObjects
            oTransObjs = ThisApplication.TransientObjects
            Dim oObjColl As ObjectCollection
            oObjColl = oTransObjs.CreateObjectCollection()

            ' Add each surfacebody of imported file to ObjectCollection
            Dim oSB As SurfaceBody
            For Each oSB In oSBs
                            oBrep.Transform(oSB, oMatrix)
                            oObjColl.Add(oSB)
            Next

            ' Get definition of current document
            Dim oPartDef As PartComponentDefinition
            oPartDef = oDoc.ComponentDefinition
            Dim oFeatures As PartFeatures
            oFeatures = oPartDef.Features
            Dim oNPFD As NonParametricBaseFeatureDefinition
            oNPFD = oFeatures.NonParametricBaseFeatures.CreateDefinition

            ' Assign the ObjectCollection to the definition.
            oNPFD.BRepEntities = oObjColl

            ' Set the output type
            oNPFD.OutputType = BaseFeatureOutputTypeEnum.kSolidOutputType

            ' Create the NonParametricBaseFeature using the definition.
            oPartDef.Features.NonParametricBaseFeatures.AddByDefinition(oNPFD)

End Sub

I found this code that renames solid bodies.

' ** Start of iLogic Code ***

Dim SB As SurfaceBody Dim Doc As PartDocument = ThisDoc.Document Dim SBs As SurfaceBodies = Doc.ComponentDefinition.SurfaceBodies Dim SBName As String = ThisDoc.FileName(False) Dim Suffix As Integer = 1

' Loop through all solid bodies, renaming them For Each SB In SBs SB.Name = SBName + "Item" & CStr(Suffix).Padleft _ (Len(CStr(Doc.ComponentDefinition.SurfaceBodies.Count)),"0") Suffix += 1 Next

' ** End of iLogic Code *****

From: Alex Fielder notifications@github.com Sent: Wednesday, May 27, 2020 6:52 AM To: AlexFielder/iLogic iLogic@noreply.github.com Cc: Cris Davis Cris.Davis@shickesteve.com; Author author@noreply.github.com Subject: Re: [AlexFielder/iLogic] Rename Part Browser Names (#7)

[cid:image001.png@01D633F7.3A8E6EC0]

Hi,

The approach is the same for Parts as is it for Assemblies; except the parent document is a PartDocument instead of the AssemblyDocument.

The following small method is from another repo on here (and relies upon System.Linq!):

Public Sub SaveCopyAsFromBrowserNodeNames()

    If TypeOf (ThisApplication.ActiveDocument) Is AssemblyDocument Then

        Dim searchstring As String = InputBox("what are we searching for?", "Search string", "Default Entry")

        If Not searchstring = String.Empty Then

            Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument

            Dim oPane As BrowserPane = oDoc.BrowserPanes("Model")

            Dim oTopNode As BrowserNode = oPane.TopNode

            Dim nodelist As List(Of String) = New List(Of String)

            nodelist = (From a As BrowserNode In oTopNode.BrowserNodes

                        Let nodedef As BrowserNodeDefinition = a.BrowserNodeDefinition

                        Where nodedef.Label.Contains(searchstring)

                        Select nodedef.Label).ToList()

            ThisApplication.StatusBarText = searchstring

            '      For Each node As browsernode In oTopnode.Browsernodes

            '              Dim nodeDef as browsernodedefinition = node.browsernodedefinition

            '              If nodedef.label.startswith(searchstring) Then

            '                     nodelist.add(nodedef.label)

            '

            '              End If

            '              MessageBox.Show("browser node: " & nodedef.label)

            '      Next

            If nodelist.Count > 0 Then

                Dim FolderName As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName)

                Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)

                For Each nodename As String In nodelist

                    Dim newfilename As String = FolderName & "\" & filename & "-" & nodename.Replace("°:1", "") & ".iam"

                    'newfilename.Replace("°:","°-")

                    MessageBox.Show(newfilename)

                    If Not System.IO.File.Exists(newfilename) Then

                        ThisApplication.ActiveDocument.SaveAs(newfilename, False)

                    End If

                Next

            End If

        End If

    End If

End Sub

As you can see, this targets an Assembly but it's fairly straightforward to rewrite to target part files.

If you can share what code you have so far I can offer some pointers on it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/AlexFielder/iLogic/issues/7#issuecomment-634608902, or unsubscribehttps://github.com/notifications/unsubscribe-auth/APXKLSPD6RRFIRAJBCLDNF3RTT5F7ANCNFSM4NK2D2RA.