Исходный код приложения

PictureDispConverter

Imports System.Runtime.InteropServices

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

Public Const PICTYPE_UNINITIALIZED As Short = -1

Public Const PICTYPE_NONE As Short = 0

Public Const PICTYPE_BITMAP As Short = 1

Public Const PICTYPE_METAFILE As Short = 2

Public Const PICTYPE_ICON As Short = 3

Public Const PICTYPE_ENHMETAFILE As Short = 4

<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

StandardAddInServer

Imports Inventor

Imports System.Runtime.InteropServices

Imports Microsoft.Win32

Namespace HelloWorldAddIn_VB

<ProgIdAttribute("HelloWorldAddIn_VB.StandardAddInServer"), _

GuidAttribute("82ed13f7-bdb1-4685-840e-6fcc84a0f84c")> _

Public Class StandardAddInServer

Implements Inventor.ApplicationAddInServer

Public m_inventorApplication As Inventor.Application

Private mAsmButtonDef As ButtonDefinition

Private mPartButtonDef As ButtonDefinition

Private Const strAddInGuid As String = "b22d0530-5e1f-426d-9a76-5b383def94fa"

#Region "ApplicationAddInServer Members"

Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate

m_inventorApplication = addInSiteObject.Application

Dim oCtrlDefs As ControlDefinitions _

= m_inventorApplication.CommandManager.ControlDefinitions

Dim oIPictureDisp As Object = PictureDispConverter _

.ToIPictureDisp(My.Resources.Icon)

mAsmButtonDef = oCtrlDefs.AddButtonDefinition( _

"Включение/выключение видимости", _

"Autodesk:RibbonVBTest:Button1", _

CommandTypesEnum.kQueryOnlyCmdType, _

strAddInGuid, _

"Описание", _

"Изменить видимость в сечении", _

oIPictureDisp, _

oIPictureDisp, _

ButtonDisplayEnum.kDisplayTextInLearningMode)

If (firstTime) Then

Dim UIManager As UserInterfaceManager _

= m_inventorApplication.UserInterfaceManager

Dim assemblyRibbon As Inventor.Ribbon _

= UIManager.Ribbons.Item("Assembly")

Dim assemblyTab As Inventor.RibbonTab _

= assemblyRibbon.RibbonTabs.Item("id_TabAssemble")

Dim panel1 As Inventor.RibbonPanel _

= assemblyTab.RibbonPanels.Add( _

"Работа с сечением", _

"Autodesk:RibbonVBTest:Panel1", _

strAddInGuid)

panel1.CommandControls.AddButton(mAsmButtonDef, True)

End If

AddHandler mAsmButtonDef.OnExecute, AddressOf Me.mAsmButtonDef_OnExecute

End Sub

#Region "Event handlers"

Private Sub mAsmButtonDef_OnExecute(ByVal Context As Inventor.NameValueMap)

Dim Assem As AssemblyDocument

Dim PartD As PartDocument

Dim oModelSettings As ModelingSettings

Assem = m_inventorApplication.ActiveDocument

If Assem.SelectSet.Count = 0 Then

MsgBox("Выберите деталь")

Return

End If

Try

Dim selec As SelectSet

selec = Assem.SelectSet

Dim detal As ComponentOccurrence = Nothing

For Each obj As Object In selec

detal = obj

PartD = detal.Definition.Document

oModelSettings = PartD.ModelingSettings

If oModelSettings.AllowSectioningThruPart = True Then

oModelSettings.AllowSectioningThruPart = False

ElseIf oModelSettings.AllowSectioningThruPart = False Then

oModelSettings.AllowSectioningThruPart = True

End If

Next

Catch

End Try

MsgBox("Видимость изменена")

End Sub

#End Region

Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate

Marshal.ReleaseComObject(m_inventorApplication)

m_inventorApplication = Nothing

System.GC.WaitForPendingFinalizers()

System.GC.Collect()

End Sub

Public ReadOnly Property Automation() As Object Implements Inventor.ApplicationAddInServer.Automation

Get

Return Nothing

End Get

End Property

Public Sub ExecuteCommand(ByVal commandID As Integer) Implements Inventor.ApplicationAddInServer.ExecuteCommand

End Sub

#End Region

End Class

End Namespace

Заключение:

В результате прохождения практики был получен опыт и знания в среде Autodesk Inventor . Было разработано приложение для автоматизации работы с 3D моделями в Autodesk Inventor. Работа выполнена в полном объеме.

Список литературы:

1) Учебный курс Autodesk: http://www.autodesk.ru/adsk/servlet/index?siteID=871736&id=20154121

2) Открытые исходные коды Autodesk: https://github.com/Developer-Autodesk/

3) Форум Autodesk: http://forums.autodesk.com

Наши рекомендации