- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am using code to check hole size and alignment of assembly. the only problem is when I click to undo I have to click it more times than I actually can. I have been trying to set up a transaction within the code so it groups the rule altogether this is my code is there any way to do this since sub commands
Sub main End Sub Sub face() Dim m_Inventor As Inventor.Application m_Inventor = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") Dim oDoc As AssemblyDocument oDoc = m_Inventor.ActiveDocument Dim oCompdef As AssemblyComponentDefinition oCompdef = oDoc.ComponentDefinition Dim oBody1 As SurfaceBody Dim oBody2 As SurfaceBody oBody1 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select first body") oBody2 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select a body to compare") Dim oFace As Face Dim oAxis As WorkAxis For Each oFace In oBody1.Faces If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then oAxis = oCompdef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector) Dim comparedFace As Face comparedFace = CompareFaces(oAxis, oBody2, oCompdef) If comparedFace Is Nothing Then oAxis.Name = oAxis.Name & "_misaligned" Else Dim oHoleFeature1 As HoleFeature oHoleFeature1 = oFace.CreatedByFeature Dim dia1 As Double dia1 = oHoleFeature1.HoleDiameter.Value dia1 = dia1 * 10 'Converting cm to mm Dim oHoleFeature2 As HoleFeature oHoleFeature2 = comparedFace.CreatedByFeature Dim dia2 As Double dia2 = oHoleFeature2.HoleDiameter.Value dia2 = dia2 * 10 'Converting cm to mm If Math.Round(dia1, 4) = Math.Round(dia2, 4) Then messagebox.show("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => OK - Default") ElseIf Math.Round(dia1, 4) >= Math.Round(dia2, 4) And Math.Round(dia1, 4) <= Math.Round(dia2, 4) + 0.5 Then messagebox.Show("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => OK - Special Type") Else messagebox.Show("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => Not OK") End If End If End If Next Dim oPane As BrowserPane oPane = oDoc.BrowserPanes.ActivePane Dim alignedNodes As ObjectCollection alignedNodes = m_Inventor.TransientObjects.CreateObjectCollection Dim misAlignedNodes As ObjectCollection misAlignedNodes = m_Inventor.TransientObjects.CreateObjectCollection For i = 4 To oCompdef.WorkAxes.Count oAxis = oCompdef.WorkAxes.Item(i) Dim misaligned As Boolean misaligned = EndsWith(oAxis.Name, "misaligned") Dim oNode As BrowserNode If misaligned = True Then oNode = oPane.GetBrowserNodeFromObject(oAxis) misAlignedNodes.Add(oNode) Else oNode = oPane.GetBrowserNodeFromObject(oAxis) alignedNodes.Add(oNode) End If Next Dim alignedAxes As BrowserFolder alignedAxes = oPane.AddBrowserFolder("Aligned Axes", alignedNodes) Dim misAlignedAxes As BrowserFolder misAlignedAxes = oPane.AddBrowserFolder("MisAligned Axes", misAlignedNodes) End Sub Public Function CompareFaces(ByVal oAxis1 As WorkAxis, ByVal oBody As SurfaceBody, ByVal compDef As AssemblyComponentDefinition) As Face For Each oFace As Face In oBody.Faces If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then Dim oAxis As WorkAxis oAxis = compDef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector) If oAxis.Line.IsColinearTo(oAxis1.Line) Then oAxis.Delete() Return oFace Exit Function Else oAxis.Delete() End If End If Next Return Nothing End Function Public Function EndsWith(Str As String, ending As String) As Boolean Dim endingLen As Integer endingLen = Len(ending) EndsWith = (Microsoft.VisualBasic.Right(Trim(UCase(Str)), endingLen) = UCase(ending)) End Function
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi again @Anonymous
Here is the simplest example of a transaction I can provide:
Option Explicit On
Public Sub Main()
If TypeOf ThisApplication.ActiveDocument Is PartDocument Then
Dim pyramidheight As String = InputBox("How high?", "Pyramid height", "10")
GeneratePyramid(pyramidheight)
Else
MessageBox.Show("Suggest you create a new part file and run this rule again!")
End If
End Sub
Public Sub GeneratePyramid(ByVal pyramidHeight As String)
Dim height As Integer = Convert.ToInt32(pyramidheight)
Dim rowCount As Integer = 1
Dim spacing As Double = 10 'units = cm
Dim oPartDocument As PartDocument = ThisApplication.ActiveDocument
Dim oCompDef As PartComponentDefinition = oPartDocument.ComponentDefinition
Dim oTrans As TransientGeometry = ThisApplication.TransientGeometry
Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(oPartDocument, "Create pyramid of points")
'i = row
'j = column
Dim XSpacing As Double = 0
Dim YSpacing As Double = 0
Dim ZSpacing As Double = 0
For i As Integer = height To 0 Step -1
'Dim tmpPoint As Point = ThisApplication.TransientGeometry.CreatePoint(0, 0, 0)
For j As Integer = 1 To i
Dim wp As WorkPoint = oCompDef.WorkPoints.AddFixed(oTrans.CreatePoint(XSpacing, YSpacing, ZSpacing))
wp.Name = "WP" & i.ToString() & "." & j.ToString()
'set up X Spacing
XSpacing += spacing
Next
'For j As Integer = 1 To rowCount
YSpacing += spacing
XSpacing = (spacing * rowCount) / 2
'Next
rowCount += 1
Next
trans.End()
End Sub
Sub updatestatusbar(ByVal message As String)
ThisApplication.statusbartext = message
End Sub
Sub updatestatusbar(ByVal percent As Double, ByVal message As String)
ThisApplication.statusbartext = message + " (" & percent.ToString("P1") + ")"
End Sub
This process is explained in more detail on the following page:
http://help.autodesk.com/view/INVNTOR/2019/ENU/?guid=GUID-991ABB26-6113-4E27-83F8-1699F259772E
![]()
Alex Fielder
Inventor Expert
https://github.com/alexfielder/
LinkedIn - Github Inventor Extension Server - Bonkers polygon iLogic thing
Top ten iLogic Tips - API Shortcut In Google Chrome - Assembly Extrusion Example
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Thank you again for all the help. The reason I posted I keep getting an error every time I try to place a transaction here is the error I keep receiving. I didn't know if i needed to place a transaction in each sub step of code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Post the code you have and I'll take a look.
Alex Fielder
Inventor Expert
https://github.com/alexfielder/
LinkedIn - Github Inventor Extension Server - Bonkers polygon iLogic thing
Top ten iLogic Tips - API Shortcut In Google Chrome - Assembly Extrusion Example
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
ok here we go
Sub Main() Dim m_Inventor As Inventor.Application m_Inventor = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") Dim oDoc As AssemblyDocument oDoc = m_Inventor.ActiveDocument Dim oCompdef As AssemblyComponentDefinition oCompdef = oDoc.ComponentDefinition Dim oBody1 As SurfaceBody Dim oBody2 As SurfaceBody oBody1 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select first body") oBody2 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select a body to compare") Dim oFace As Face Dim oAxis As WorkAxis Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(oPartDocument, "Hole Alignment") For Each oFace In oBody1.Faces If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then oAxis = oCompdef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector) Dim comparedFace As Face comparedFace = CompareFaces(oAxis, oBody2, oCompdef) If comparedFace Is Nothing Then oAxis.Name = oAxis.Name & "_misaligned" Else Dim oHoleFeature1 As HoleFeature oHoleFeature1 = oFace.CreatedByFeature Dim dia1 As Double dia1 = oHoleFeature1.HoleDiameter.Value dia1 = dia1 * 10 'Converting cm to mm Dim oHoleFeature2 As HoleFeature oHoleFeature2 = comparedFace.CreatedByFeature Dim dia2 As Double dia2 = oHoleFeature2.HoleDiameter.Value dia2 = dia2 * 10 'Converting cm to mm If Math.Round(dia1, 4) = Math.Round(dia2, 4) Then messagebox.Show("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => OK - Default") ElseIf Math.Round(dia1, 4) >= Math.Round(dia2, 4) And Math.Round(dia1, 4) <= Math.Round(dia2, 4) + 0.5 Then messagebox.Show("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => OK - Special Type") Else messagebox.Show("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => Not OK") End If End If End If Next Dim oPane As BrowserPane oPane = oDoc.BrowserPanes.ActivePane Dim alignedNodes As ObjectCollection alignedNodes = m_Inventor.TransientObjects.CreateObjectCollection Dim misAlignedNodes As ObjectCollection misAlignedNodes = m_Inventor.TransientObjects.CreateObjectCollection For i = 4 To oCompdef.WorkAxes.Count oAxis = oCompdef.WorkAxes.Item(i) Dim misaligned As Boolean misaligned = EndsWith(oAxis.Name, "misaligned") Dim oNode As BrowserNode If misaligned = True Then oNode = oPane.GetBrowserNodeFromObject(oAxis) misAlignedNodes.Add(oNode) Else oNode = oPane.GetBrowserNodeFromObject(oAxis) alignedNodes.Add(oNode) End If Next Dim alignedAxes As BrowserFolder alignedAxes = oPane.AddBrowserFolder("Aligned Axes", alignedNodes) Dim misAlignedAxes As BrowserFolder misAlignedAxes = oPane.AddBrowserFolder("MisAligned Axes", misAlignedNodes) trans.end End Sub Public Function CompareFaces(ByVal oAxis1 As WorkAxis, ByVal oBody As SurfaceBody, ByVal compDef As AssemblyComponentDefinition) As Face For Each oFace As Face In oBody.Faces If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then Dim oAxis As WorkAxis oAxis = compDef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector) If oAxis.Line.IsColinearTo(oAxis1.Line) Then oAxis.Delete() Return oFace Exit Function Else oAxis.Delete() End If End If Next Return Nothing End Function Public Function EndsWith(Str As String, ending As String) As Boolean Dim endingLen As Integer endingLen = Len(ending) EndsWith = (Microsoft.VisualBasic.Right(Trim(UCase(Str)), endingLen) = UCase(ending)) End Function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Does that work then? It looks like it will to my eyes.
Alex Fielder
Inventor Expert
https://github.com/alexfielder/
LinkedIn - Github Inventor Extension Server - Bonkers polygon iLogic thing
Top ten iLogic Tips - API Shortcut In Google Chrome - Assembly Extrusion Example
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
From everything i was reading, I thought I was doing it right. but I still was getting the same error.
(is there a simple way to go from vba to vba.net)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
You've got one error which can be fixed like this:
Change this:
Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(oPartDocument, "Hole Alignment")
To this:
Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(oDoc, "Hole Alignment")
![]()
Alex Fielder
Inventor Expert
https://github.com/alexfielder/
LinkedIn - Github Inventor Extension Server - Bonkers polygon iLogic thing
Top ten iLogic Tips - API Shortcut In Google Chrome - Assembly Extrusion Example
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report