Autodesk Inventor: Tạo Lệnh Tùy Chỉnh Invert Visibility Thêm Vào Ribbon Inventor

Trong khi thao tác với các bộ phận lắp ráp Assembly, việc ẩn và hiện các chi tiết để dễ quan sát các chi tiết bên trong là rất thường xuyên. Phần mềm CATIA có một lệnh rất tiện trong quá trình thiết kế giúp đảo ẩn hiện chi tiết Component, mà trong Inventor chỉ có lệnh Isolate, không tiện lợi bằng. Lệnh trong phần mềm CATIA này có chức năng là ẩn những component đang hiện và hiện những component đang ẩn. Bài viết này sẽ hướng dẫn tạo một nút lệnh có chức năng tương tự như vậy, đặt tên là Invert Visibility và một nút lệnh Show All để hiện tất cả component. Sau đó thêm chúng vào một tab ribbon mới tự tạo tên là 自分ツール của Inventor. Có thể mở rộng chức năng lệnh này ra, là ẩn hiện các yếu tố hỗ trợ dựng hình như điểm, đường thẳng, mặt phẳng, UCS,… trong file chi tiết part.

Tạo một dự án Visual Studio mới với Template Add-In Inventor

Khởi động Visual Studio và chọn tạo project mới.

Để có Template Add-In Inventor hiện trong ô tìm kiếm, ta phải cài SDK Inventor như hướng dẫn của bài viết trước. https://z-log.net/2024/04/13/autodesk-inventor-ket-noi-visual-studio-va-inventor-sdk-de-tao-add-in/

Đặt tên Project và đường dẫn chứa các file nguồn.

Thiết lập ban đầu.

Chọn .NET Framework, đối với Inventor 2020 .NET Frameworks phải có phiên bản 4.7 trở lên.

Thiết lập nơi đặt file sau khi biên dịch. Vào đường dẫn %APPDATA%\Autodesk\ApplicationPlugins <=> C:\Users\ZBook\AppData\Roaming\Autodesk\ApplicationPlugins\, tạo Thư mục <InventorAddIn> chứa file biên dịch Add-In.

Xóa các mục trong Build Event..

Chọn file debug ở đường dẫn: C:\Program Files\Autodesk\Inventor 2020\Bin\Inventor.exe.

Add file ảnh .png để làm icon, file lớn kích thước 32×32 pixel, file nhỏ 16×16 pixel.

Thông tin 2 file chính của Mẫu Add-In

File Autodesk.InventorAddIn.Inventor.addin

<Addin Type="Standard">
  <!--Created for Autodesk Inventor Version 24.0-->
  <ClassId>{86d7afa3-3bf3-4db3-a1f8-58e92d6e05a3}</ClassId>
  <ClientId>{86d7afa3-3bf3-4db3-a1f8-58e92d6e05a3}</ClientId>
  <DisplayName>InventorAddIn</DisplayName>
  <Description>InventorAddIn</Description>
  <Assembly>InventorAddIn.dll</Assembly>
  <LoadOnStartUp>1</LoadOnStartUp>
  <UserUnloadable>1</UserUnloadable>
  <Hidden>0</Hidden>
  <SupportedSoftwareVersionGreaterThan>23..</SupportedSoftwareVersionGreaterThan>
  <DataVersion>1</DataVersion>
  <UserInterfaceVersion>1</UserInterfaceVersion>
</Addin>

File StandardAddInServer.vb

Imports System.Runtime.InteropServices
Imports Inventor

Namespace InventorAddIn
    <ProgIdAttribute("InventorAddIn.StandardAddInServer"),
    GuidAttribute("86d7afa3-3bf3-4db3-a1f8-58e92d6e05a3")>
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

        Private WithEvents m_uiEvents As UserInterfaceEvents
        Private WithEvents InvertVisibilityButton, ShowAllButton As ButtonDefinition

#Region "ApplicationAddInServer Members"

        ' This method is called by Inventor when it loads the AddIn. The AddInSiteObject provides access  
        ' to the Inventor Application object. The FirstTime flag indicates if the AddIn is loaded for
        ' the first time. However, with the introduction of the ribbon this argument is always true.
        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
            ' Initialize AddIn members.
            g_inventorApplication = addInSiteObject.Application

            ' Connect to the user-interface events to handle a ribbon reset.
            m_uiEvents = g_inventorApplication.UserInterfaceManager.UserInterfaceEvents

            ' TODO: Add button definitions.

            ' Sample to illustrate creating a button definition.
            Dim largeIconInvert As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.inverticon32x32)
            Dim smallIconInvert As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.inverticon16x16)
            Dim largeIconShowAll As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.ShowAllicon32x32)
            Dim smallIconShowAll As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.ShowAllicon16x16)

            Dim controlDefs As Inventor.ControlDefinitions = g_inventorApplication.CommandManager.ControlDefinitions
            InvertVisibilityButton = controlDefs.AddButtonDefinition("Invert Visibility", "InvertVisibility", CommandTypesEnum.kShapeEditCmdType, AddInClientID, "Đảo Ẩn/Hiện Component", "Đảo Ẩn/Hiện Component", smallIconInvert, largeIconInvert)
            ShowAllButton = controlDefs.AddButtonDefinition("Show All Component", "ShowAll", CommandTypesEnum.kShapeEditCmdType, AddInClientID, "Hiện Tất Cả Component", "Hiện Tất Cả Component", smallIconShowAll, largeIconShowAll)

            ' Add to the user interface, if it's the first time.
            If firstTime Then
                AddToUserInterface()
            End If
        End Sub

        ' This method is called by Inventor when the AddIn is unloaded. The AddIn will be
        ' unloaded either manually by the user or when the Inventor session is terminated.
        Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate

            ' TODO:  Add ApplicationAddInServer.Deactivate implementation

            ' Release objects.
            m_uiEvents = Nothing
            g_inventorApplication = Nothing

            System.GC.Collect()
            System.GC.WaitForPendingFinalizers()
        End Sub

        ' This property is provided to allow the AddIn to expose an API of its own to other 
        ' programs. Typically, this  would be done by implementing the AddIn's API
        ' interface in a class and returning that class object through this property.
        Public ReadOnly Property Automation() As Object Implements Inventor.ApplicationAddInServer.Automation
            Get
                Return Nothing
            End Get
        End Property

        ' Note:this method is now obsolete, you should use the 
        ' ControlDefinition functionality for implementing commands.
        Public Sub ExecuteCommand(ByVal commandID As Integer) Implements Inventor.ApplicationAddInServer.ExecuteCommand
        End Sub

#End Region

#Region "User interface definition"
        ' Sub where the user-interface creation is done.  This is called when
        ' the add-in loaded and also if the user interface is reset.
        Private Sub AddToUserInterface()
            ' This is where you'll add code to add buttons to the ribbon.

            '** Sample to illustrate creating a button on a new panel of the Tools tab of the Part ribbon.

            '' Get the part ribbon.
            'Dim partRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("Part")

            '' Get the assembly ribbon.
            Dim assemblyRibbon As Ribbon = g_inventorApplication.UserInterfaceManager.Ribbons.Item("Assembly")
            '' Get the "Tools" tab.
            'Dim toolsTab As RibbonTab = partRibbon.RibbonTabs.Item("id_TabTools")
            Dim assemblyNewTab As RibbonTab = assemblyRibbon.RibbonTabs.Add("自分ツール", "id_CustomAddins", AddInClientID)

            '' Create a new panel.
            'Dim customPanel As RibbonPanel = toolsTab.RibbonPanels.Add("Sample", "MysSample", AddInClientID)
            Dim assemblyCustomPanel As RibbonPanel = Nothing
            Try
                assemblyCustomPanel = assemblyNewTab.RibbonPanels.Item("InvertVisibility")
            Catch ex As Exception
            End Try

            If assemblyCustomPanel Is Nothing Then
                assemblyCustomPanel = assemblyNewTab.RibbonPanels.Add("Invert Visibility", "InvertVisibility", AddInClientID)
            End If
            '' Add a button.
            'customPanel.CommandControls.AddButton(m_sampleButton)
            assemblyCustomPanel.CommandControls.AddButton(InvertVisibilityButton, True)
            assemblyCustomPanel.CommandControls.AddButton(ShowAllButton, True)

        End Sub

        Private Sub m_uiEvents_OnResetRibbonInterface(Context As NameValueMap) Handles m_uiEvents.OnResetRibbonInterface
            ' The ribbon was reset, so add back the add-ins user-interface.
            AddToUserInterface()
        End Sub

        ' Sample handler for the button.
        Private Sub InvertVisibility_OnExecute(Context As NameValueMap) Handles InvertVisibilityButton.OnExecute
            Try
                InvertVisibility()
            Catch ex As Exception
                MsgBox("Something went wrong while runing rule. Message: " & ex.Message)
            End Try
        End Sub

        Private Sub ShowAll_OnExecute(Context As NameValueMap) Handles ShowAllButton.OnExecute
            Try
                ShowAllComponents()
            Catch ex As Exception
                MsgBox("Something went wrong while runing rule. Message: " & ex.Message)
            End Try
        End Sub
#End Region
    End Class
End Namespace


Public Module Globals
    ' Inventor application object.
    Public g_inventorApplication As Inventor.Application

#Region "Function to get the add-in client ID."
    ' This function uses reflection to get the GuidAttribute associated with the add-in.
    Public Function AddInClientID() As String
        Dim guid As String = ""
        Try
            Dim t As Type = GetType(InventorAddIn.StandardAddInServer)
            Dim customAttributes() As Object = t.GetCustomAttributes(GetType(GuidAttribute), False)
            Dim guidAttribute As GuidAttribute = CType(customAttributes(0), GuidAttribute)
            guid = "{" + guidAttribute.Value.ToString() + "}"
        Catch
        End Try

        Return guid
    End Function
#End Region

#Region "hWnd Wrapper Class"
    ' This class is used to wrap a Win32 hWnd as a .Net IWind32Window class.
    ' This is primarily used for parenting a dialog to the Inventor window.
    '
    ' For example:
    ' myForm.Show(New WindowWrapper(g_inventorApplication.MainFrameHWND))
    '
    Public Class WindowWrapper
        Implements System.Windows.Forms.IWin32Window
        Public Sub New(ByVal handle As IntPtr)
            _hwnd = handle
        End Sub

        Public ReadOnly Property Handle() As IntPtr _
          Implements System.Windows.Forms.IWin32Window.Handle
            Get
                Return _hwnd
            End Get
        End Property

        Private _hwnd As IntPtr
    End Class
#End Region

#Region "Image Converter"
    ' Class used to convert bitmaps and icons from their .Net native types into
    ' an IPictureDisp object which is what the Inventor API requires. A typical
    ' usage is shown below where MyIcon is a bitmap or icon that's available
    ' as a resource of the project.
    '
    ' Dim smallIcon As stdole.IPictureDisp = PictureDispConverter.ToIPictureDisp(My.Resources.MyIcon)

    Public NotInheritable Class PictureDispConverter
        <DllImport("OleAut32.dll", EntryPoint:="OleCreatePictureIndirect", ExactSpelling:=True, PreserveSig:=False)> _
        Private Shared Function OleCreatePictureIndirect( _
            <MarshalAs(UnmanagedType.AsAny)> ByVal picdesc As Object, _
            ByRef iid As Guid, _
            <MarshalAs(UnmanagedType.Bool)> ByVal fOwn As Boolean) As stdole.IPictureDisp
        End Function

        Shared iPictureDispGuid As Guid = GetType(stdole.IPictureDisp).GUID

        Private NotInheritable Class PICTDESC
            Private Sub New()
            End Sub

            'Picture Types
            Public Const PICTYPE_BITMAP As Short = 1
            Public Const PICTYPE_ICON As Short = 3

            <StructLayout(LayoutKind.Sequential)> _
            Public Class Icon
                Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Icon))
                Friend picType As Integer = PICTDESC.PICTYPE_ICON
                Friend hicon As IntPtr = IntPtr.Zero
                Friend unused1 As Integer
                Friend unused2 As Integer

                Friend Sub New(ByVal icon As System.Drawing.Icon)
                    Me.hicon = icon.ToBitmap().GetHicon()
                End Sub
            End Class

            <StructLayout(LayoutKind.Sequential)> _
            Public Class Bitmap
                Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Bitmap))
                Friend picType As Integer = PICTDESC.PICTYPE_BITMAP
                Friend hbitmap As IntPtr = IntPtr.Zero
                Friend hpal As IntPtr = IntPtr.Zero
                Friend unused As Integer

                Friend Sub New(ByVal bitmap As System.Drawing.Bitmap)
                    Me.hbitmap = bitmap.GetHbitmap()
                End Sub
            End Class
        End Class

        Public Shared Function ToIPictureDisp(ByVal icon As System.Drawing.Icon) As stdole.IPictureDisp
            Dim pictIcon As New PICTDESC.Icon(icon)
            Return OleCreatePictureIndirect(pictIcon, iPictureDispGuid, True)
        End Function

        Public Shared Function ToIPictureDisp(ByVal bmp As System.Drawing.Bitmap) As stdole.IPictureDisp
            Dim pictBmp As New PICTDESC.Bitmap(bmp)
            Return OleCreatePictureIndirect(pictBmp, iPictureDispGuid, True)
        End Function
    End Class
#End Region

End Module


Module Module1
    Sub InvertVisibility()
        Dim inventorApp As Inventor.Application = GetInventorApplication()
        If inventorApp Is Nothing Then
            Console.WriteLine("Autodesk Inventor is not running.")
            Return
        End If

        Dim activeDocument As Document = inventorApp.ActiveDocument
        If activeDocument Is Nothing Then
            Console.WriteLine("No active document.")
            Return
        End If

        If TypeOf activeDocument Is AssemblyDocument Then
            Dim asmDoc As AssemblyDocument = CType(activeDocument, AssemblyDocument)
            InvertComponentVisibility(asmDoc)
            Console.WriteLine("Component visibility inverted.")
        Else
            Console.WriteLine("Active document is not an assembly document.")
        End If
    End Sub

    Sub ShowAllComponents()
        Dim inventorApp As Inventor.Application = GetInventorApplication()
        If inventorApp Is Nothing Then
            Console.WriteLine("Autodesk Inventor is not running.")
            Return
        End If

        Dim activeDocument As Document = inventorApp.ActiveDocument
        If activeDocument Is Nothing Then
            Console.WriteLine("No active document.")
            Return
        End If

        If TypeOf activeDocument Is AssemblyDocument Then
            Dim asmDoc As AssemblyDocument = CType(activeDocument, AssemblyDocument)
            For Each comp As ComponentOccurrence In asmDoc.ComponentDefinition.Occurrences
                comp.Visible = True
            Next
            Console.WriteLine("All components are now visible.")
        Else
            Console.WriteLine("Active document is not an assembly document.")
        End If
    End Sub

    Function GetInventorApplication() As Inventor.Application
        Try
            Dim inventorApp As Inventor.Application = Marshal.GetActiveObject("Inventor.Application")
            Return inventorApp
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Sub InvertComponentVisibility(ByVal asmDoc As AssemblyDocument)
        For Each comp As ComponentOccurrence In asmDoc.ComponentDefinition.Occurrences
            comp.Visible = Not comp.Visible
        Next
    End Sub
End Module

Unlock Add-In

Sau khi Save All và Build Project. Nhấn vào Nút Start để chạy Debug. Nếu lần đầu chạy thành công sẽ xuất hiện bảng thống báo Bảo mật như hình dưới. Bỏ chọn Block và chọn Loaded/Unloaded và Load Automatically. Nhấn OK. Lần sau khi khởi động Inventor thì Add sẽ tự động load vào giao diện người dùng.

Sử dụng Add-In

Ở bước thiết lập Project, ta chọn đường dẫn Compile là : C:\Users\ZBook\AppData\Roaming\Autodesk\ApplicationPlugins\ hoặc (%APPDATA%\Autodesk\ApplicationPlugins). Thư mục này sẽ được Inventor quét tìm Add-In khi khởi động.

Nếu bạn đặt file biên dịch ở thư mục mà Inventor không quét Add-In thì bạn copy 2 file sau trong thư mục biên dịch vào thư mục trên. Autodesk.InventorAddIn.Inventor.addin, InventorAddIn.dll.

Hoặc muốn gửi Add-In tới máy tính khác thì cũng làm tương tự. Copy 2 file trên tới thư mục quét Add-In (Trong Window 7/8.1/10 – %APPDATA%\Autodesk\ApplicationPlugins) của máy cần chuyển tới.

Hoàn thành

Sau khi tạo và chạy thành công Add-In, khi khởi động Inventor sẽ xuất hiện giao diện với Ribbon Tab và 2 nút lệnh như hình bên dưới.

Bình luận

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *