Wish List simplification program

Wish List simplification program

andrew_canfield
Collaborator Collaborator
795 Views
9 Replies
Message 1 of 10

Wish List simplification program

andrew_canfield
Collaborator
Collaborator

Hello

Wondered how long it would take me to learn how to create a program which created a boundary cube & pasted an image on the surfaces - so the cube could be easily identified.

This kind of thing ...cubewrap.jpg

 

Regards

 

Andrew

0 Likes
796 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor

@andrew_canfield 

I did a little work on a very similar idea using a regular iLogic external rule, a while back, but never finished it.

It was just going to create a seperate part file with the box created in it, and save it with the same name, and location as the main assembly file.

It's probably a good start for someone to pick up where I left off though.

I seem to remember not finding an easy way to use an in-memory IPictureDisp (without saving the image to disk) to use it for the images on the Faces of the resulting box.  I also played around with just going ahead and saving the BMP images to the C:\Temp folder, then using them later to paste on the faces, then to delete them, when done.

But as I stated before, it's not a working code at this point.

Here it is.

Imports Inventor
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
AddReference "stdole.dll"
AddReference "System.Drawing"


Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oBox As Box = oADef.RangeBox
Dim oMinPoint As Point = oBox.MinPoint
Dim oMaxPoint As Point = oBox.MaxPoint
Dim oXDist As Double = oMaxPoint.X - oMinPoint.X
Dim oYDist As Double = oMaxPoint.Y - oMinPoint.Y
Dim oZDist As Double = oMaxPoint.Z - oMinPoint.Z
MessageBox.Show("The RangeBox of this AssemblyDocument is:" & vbNewLine &
oXDist & " along X-Axis" & vbNewLine &
oYDist & " along Y-Axis" & vbNewLine &
oZDist & " along Z-Axis")

'Front View
Dim oFrontView As Inventor.View = oADoc.Views.Add()
oFrontView.Caption = "FRONT VIEW"
oFrontView.DisplayMode = DisplayModeEnum.kRealisticRendering
oFrontView.Fit(True)
oFrontView.Visible = True
Dim oFVCamera As Camera = oFrontView.Camera
oFVCamera.ViewOrientationType = ViewOrientationTypeEnum.kFrontViewOrientation
oFVCamera.Fit
oFVCamera.Apply
Dim oFViewPic As IPictureDisp = oFVCamera.CreateImage(Int(oXDist)*10, Int(oYDist)*10) 'oWidth & oHeight are pixel counts

'Right View
Dim oRightView As Inventor.View = oADoc.Views.Add()
oRightView.Caption = "RIGHT VIEW"
oRightView.DisplayMode = DisplayModeEnum.kRealisticRendering
oRightView.Fit(True)
oRightView.Visible = True
Dim oRVCamera As Camera = oRightView.Camera
oRVCamera.ViewOrientationType = ViewOrientationTypeEnum.kRightViewOrientation
oRVCamera.Fit
oRVCamera.Apply
Dim oRViewPic As IPictureDisp = oRVCamera.CreateImage(Int(oYDist)*10, Int(oZDist)*10) 'oWidth & oHeight are pixel counts

'Left View
Dim oLeftView As Inventor.View = oADoc.Views.Add()
oLeftView.Caption = "LEFT VIEW"
oLeftView.DisplayMode = DisplayModeEnum.kRealisticRendering
oLeftView.Fit(True)
oLeftView.Visible = True
Dim oLVCamera As Camera = oLeftView.Camera
oLVCamera.ViewOrientationType = ViewOrientationTypeEnum.kLeftViewOrientation
oLVCamera.Fit
oLVCamera.Apply
Dim oLViewPic As IPictureDisp = oLVCamera.CreateImage(Int(oYDist)*10, Int(oZDist)*10) 'oWidth & oHeight are pixel counts

'Back View
Dim oBackView As Inventor.View = oADoc.Views.Add()
oBackView.Caption = "BACK VIEW"
oBackView.DisplayMode = DisplayModeEnum.kRealisticRendering
oBackView.Fit(True)
oBackView.Visible = True
Dim oBKVCamera As Camera = oBackView.Camera
oBKVCamera.ViewOrientationType = ViewOrientationTypeEnum.kBackViewOrientation
oBKVCamera.Fit
oBKVCamera.Apply
Dim oBKViewPic As IPictureDisp = oBKVCamera.CreateImage(Int(oXDist)*10, Int(oYDist)*10) 'oWidth & oHeight are pixel counts

'Top View
Dim oTopView As Inventor.View = oADoc.Views.Add()
oTopView.Caption = "TOP VIEW"
oTopView.DisplayMode = DisplayModeEnum.kRealisticRendering
oTopView.Fit(True)
oTopView.Visible = True
Dim oTVCamera As Camera = oTopView.Camera
oTVCamera.ViewOrientationType = ViewOrientationTypeEnum.kTopViewOrientation
oTVCamera.Fit
oTVCamera.Apply
'Dim oTopViewPic As IPictureDisp = oTVCamera.CreateImage(Int(oXDist) * 10, Int(oZDist) * 10) 'oWidth & oHeight are pixel counts
Dim oBMP_Path As String = "C:\Temp\"
Dim oBMP_Name As String = "TOP VIEW IMAGE.bmp"
oTVCamera.SaveAsBitmap(oBMP_Path & oBMP_Name,Int(oXDist) * 10,Int(oZDist) * 10)

'Bottom View
Dim oBottomView As Inventor.View = oADoc.Views.Add()
oBottomView.Caption = "BOTTOM VIEW"
oBottomView.DisplayMode = DisplayModeEnum.kRealisticRendering
oBottomView.Fit(True)
oBottomView.Visible = True
Dim oBTVCamera As Camera = oBottomView.Camera
oBTVCamera.ViewOrientationType = ViewOrientationTypeEnum.kBottomViewOrientation
oBTVCamera.Fit
oBTVCamera.Apply
Dim oBTViewPic As IPictureDisp = oBTVCamera.CreateImage(Int(oXDist)*10, Int(oZDist)*10) 'oWidth & oHeight are pixel counts

Dim oTG As TransientGeometry = ThisApplication.TransientGeometry

'Create a Part File to create the physical box in.
Dim oBoxDoc As PartDocument
Dim oPTPath As String = ThisApplication.FileOptions.TemplatesPath
oBoxDoc = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject,oPTPath & "PART.ipt",True)
Dim oBoxDef As PartComponentDefinition = oBoxDoc.ComponentDefinition
Dim oSketches As PlanarSketches = oBoxDef.Sketches
Dim oBaseSketch As PlanarSketch = oSketches.Add(oBoxDef.WorkPlanes.Item("XZ Plane"))
oBaseSketch.Name = "Simplified Part Base Sketch"
oBaseSketch.SketchLines.AddAsTwoPointRectangle(oTG.CreatePoint2d(oMinPoint.X, oMinPoint.Z), oTG.CreatePoint2d(oMaxPoint.X, oMaxPoint.Z))
oBaseSketch.Profiles.AddForSolid
Dim oExtFeats As ExtrudeFeatures = oBoxDef.Features.ExtrudeFeatures
Dim oExtFeatDef As ExtrudeDefinition = oExtFeats.CreateExtrudeDefinition(oBaseSketch.Profiles.Item(1), PartFeatureOperationEnum.kNewBodyOperation)
oExtFeatDef.SetDistanceExtent(oYDist, PartFeatureExtentDirectionEnum.kPositiveExtentDirection)
Dim oExtFeat As ExtrudeFeature = oExtFeats.Add(oExtFeatDef)
oExtFeat.Name = oADoc.DisplayName & " - Simplified"

'Start identifying the faces & setting the images
Dim oDecals As DecalFeatures = oBoxDef.Features.DecalFeatures
Dim oDecal As DecalFeature
Dim oFaceProfile As PlanarSketch

Dim oNE = iLogicVb.Automation.GetNamedEntities(oBoxDoc)

Dim oTopFace As Face = oExtFeat.EndFaces.Item(1)
oNE.SetName(oTopFace,"TOP FACE")
oFaceProfile = oSketches.Add(oTopFace, True)
oFaceProfile.Name = "TOP FACE PROFILE SKETCH"
For Each oEntity As SketchEntity In oFaceProfile.SketchEntities
	oEntity.Construction = False
Next
'The following line tries to use the IPictureDisp defined above in place of the FullFileName of an Image File.
'Dim oTopPic As SketchImage = oFaceProfile.SketchImages.Add(oTopViewPic, oTG.CreatePoint2d(oMinPoint.X, oMinPoint.Z), False)
Dim oTopPic As SketchImage = oFaceProfile.SketchImages.Add(oBMP_Path & oBMP_Name, oTG.CreatePoint2d(oMinPoint.X, oMinPoint.Z), False)
oTopPic.Width = oXDist
oTopPic.Height = oZDist
oDecal = oDecals.Add(oTopPic, oTopFace, False, False)
oDecal.Name = "TOP FACE DECAL"
Dim oBottomFace As Face = oExtFeat.StartFaces.Item(1)
For Each oFace As Face In oExtFeat.SideFaces
	'If oFace.
	'oFaceProfile = oSketches.Add(oFace, True)
	'oFaceProfile.Name = ""
'	oFace.AppearanceSourceType = AppearanceSourceTypeEnum.kOverrideAppearance
'	oFace.Appearance = 
Next

I hope this helps you on your way.  Post back if you get it working.  Good luck.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 10

andrew_canfield
Collaborator
Collaborator

Hello - pretty substantial project!

 

I was speaking to an engineer yesterday who had created a part with images on - pretty sure automating this task would benefit a few people.

 

If the images could be 'vectorised ' & the cube edges hidden the dummy parts could be used in drawings - where small details aren't required. Maybe this is a 'Flatshot' - just an idea.

 

Thanks for the code.

 

Regards

 

Andrew

0 Likes
Message 4 of 10

tdant
Collaborator
Collaborator

I figured I'd benefit from this, so I took to it. I started in VBA, but wanted functionality of VB.Net, so it turned into an add-in. Here's a link to the .msi installer on my Google Drive. It adds a new button to the Simplification panel of the Assemble tab in an assembly. Clicking that button creates a new part file with screenshots pasted on each side, saved as the active assembly's name appended with "Simplified Box", and inserts it into the assembly in the correct position and orientation. It also creates two new LOD representations to handle the new part. Play around with it, and see if it's helpful.

Message 5 of 10

GeorgK
Advisor
Advisor

@tdantIs it possible that you post the source code?

 

Thank you

Georg

0 Likes
Message 6 of 10

tdant
Collaborator
Collaborator

Here are the important bits:

Private Sub m_simplifyButton_OnExecute(Context As NameValueMap) Handles m_simplifyButton.OnExecute

            ' Get the active assembly document
            Try
                topAsm = g_inventorApplication.ActiveDocument
            Catch ex As Exception
                MsgBox("No assembly document detected. Please activate an assembly document and try again.", Title:="Error")
                Exit Sub
            End Try

            topAsm = g_inventorApplication.ActiveDocument

            Dim topCompDef As AssemblyComponentDefinition = topAsm.ComponentDefinition

            Dim topLocation As String = Left(topAsm.FullFileName, InStrRev(topAsm.FullFileName, "\"))

            Dim LODDoc As PartDocument = g_inventorApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, CreateVisible:=False)
            LODDoc.SaveAs(topLocation & topAsm.DisplayName & ", Simplified Box.ipt", False)

            Dim LODCompDef As PartComponentDefinition = LODDoc.ComponentDefinition

            Dim xySketch As PlanarSketch = LODCompDef.Sketches.Add(LODCompDef.WorkPlanes("XY Plane"))

            Dim cornerPoint1 As Point2d = g_inventorApplication.TransientGeometry.CreatePoint2d(topCompDef.RangeBox.MinPoint.X, topCompDef.RangeBox.MinPoint.Y)

            Dim cornerPoint2 As Point2d = g_inventorApplication.TransientGeometry.CreatePoint2d(topCompDef.RangeBox.MaxPoint.X, topCompDef.RangeBox.MaxPoint.Y)

            xySketch.SketchLines.AddAsTwoPointRectangle(cornerPoint1, cornerPoint2)

            Dim extrudeProfile As Profile = xySketch.Profiles.AddForSolid()

            Dim negativeZFace As WorkPlane = LODCompDef.WorkPlanes.AddByPlaneAndOffset(LODCompDef.WorkPlanes("XY Plane"), topCompDef.RangeBox.MinPoint.Z)
            Dim positiveZFace As WorkPlane = LODCompDef.WorkPlanes.AddByPlaneAndOffset(LODCompDef.WorkPlanes("XY Plane"), topCompDef.RangeBox.MaxPoint.Z)
            negativeZFace.Visible = False
            positiveZFace.Visible = False

            Dim LODExtrudeDef As ExtrudeDefinition = LODCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(extrudeProfile, PartFeatureOperationEnum.kNewBodyOperation)
            LODExtrudeDef.SetFromToExtent(negativeZFace, True, positiveZFace, True)
            LODCompDef.Features.ExtrudeFeatures.Add(LODExtrudeDef)
            LODDoc.Save()

            Dim activeView As View = g_inventorApplication.ActiveView

            Dim wasCamPerspective As Boolean = g_inventorApplication.ActiveView.Camera.Perspective

            controlDefs("AppOrthographicCameraCmd").Execute()

            ' *** start of decal creation routine
            CreateDecals(LODCompDef)

            LODCompDef.BOMStructure = BOMStructureEnum.kReferenceBOMStructure

            controlDefs("AppIsometricViewCmd").Execute()

            If wasCamPerspective Then controlDefs("AppPerspectiveCameraCmd").Execute()

            ' Place new part grounded in topAsm
            Dim originMatrix As Matrix = g_inventorApplication.TransientGeometry.CreateMatrix()
            Dim LODOcc As ComponentOccurrence = topCompDef.Occurrences.Add(LODDoc.FullDocumentName, originMatrix)
            LODOcc.Grounded = True

            ' Create new LODReps
            Try
                topCompDef.RepresentationsManager.LevelOfDetailRepresentations("Default").Activate()
            Catch
                topCompDef.RepresentationsManager.LevelOfDetailRepresentations.Add("Default").Activate()
            End Try
            LODOcc.Suppress()
            topAsm.Save()

            Try
                topCompDef.RepresentationsManager.LevelOfDetailRepresentations("Simplified Box").Activate()
            Catch
                topCompDef.RepresentationsManager.LevelOfDetailRepresentations.Add("Simplified Box").Activate()
            End Try

            For Each gOcc As ComponentOccurrence In topCompDef.Occurrences
                If Not gOcc Is LODOcc Then gOcc.Suppress()
            Next
            LODOcc.Unsuppress()
            topAsm.Save()

            topAsm = Nothing
        End Sub

Function CropBitmapImage(imgName As String) As Drawing.Bitmap
        ' Crop white background out of image
        Dim imgBitmap As Drawing.Bitmap = New Drawing.Bitmap(imgName)
        Dim xMax As Integer = imgBitmap.Width - 1
        Dim yMax As Integer = imgBitmap.Height - 1
        Dim x As Integer
        Dim y As Integer
        Dim xLeft As Integer = xMax
        Dim xRight As Integer = 0
        Dim topSet As Boolean = False
        Dim yTop As Integer
        Dim bottomSet As Boolean = False
        Dim yBottom As Integer = 0

        For y = 0 To yMax
            For x = 0 To xMax
                With imgBitmap.GetPixel(x, y)
                    If .R = 255 And .G = 255 And .B = 255 Then Continue For
                End With

                If Not topSet Then
                    yTop = y
                    topSet = True
                End If

                If x < xLeft Then xLeft = x
            Next
        Next

        For y = yMax To 0 Step -1
            For x = xMax To 0 Step -1
                With imgBitmap.GetPixel(x, y)
                    If .R = 255 And .G = 255 And .B = 255 Then Continue For

                    If Not bottomSet Then
                        yBottom = y
                        bottomSet = True
                    End If

                    If x > xRight Then xRight = x

                End With
            Next
        Next

        Dim cropWindow As Drawing.Rectangle = New Drawing.Rectangle(xLeft, yTop, xRight - xLeft, yBottom - yTop)

        Return imgBitmap.Clone(cropWindow, Drawing.Imaging.PixelFormat.Format16bppRgb555)

        imgBitmap.Dispose()
    End Function

    Function GetImageInsertPoint(gSketch As PlanarSketch) As Object
        Dim first As Boolean = True, xVal As Double, yVal As Double
        For Each gPoint As SketchPoint In gSketch.SketchPoints
            If first Then
                xVal = gPoint.Geometry.X
                yVal = gPoint.Geometry.Y
                first = False
                Continue For
            End If

            If gPoint.Geometry.X < xVal Then xVal = gPoint.Geometry.X
            If gPoint.Geometry.Y > yVal Then yVal = gPoint.Geometry.Y
        Next

        Return {xVal, yVal}
    End Function

    Function GetSketchDimensions(gSketch As PlanarSketch) As Object
        Dim xMin As Double, xMax As Double, yMin As Double, yMax As Double
        Dim listXVals As New Generic.List(Of Double), listYVals As New Generic.List(Of Double)
        For i = 1 To gSketch.SketchPoints.Count
            With gSketch.SketchPoints(i).Geometry
                listXVals.Add(.X)
                listYVals.Add(.Y)
            End With
        Next

        listXVals.Sort()
        listYVals.Sort()

        xMin = listXVals(0)
        xMax = listXVals(listXVals.Count - 1)
        yMin = listYVals(0)
        yMax = listYVals(listYVals.Count - 1)

        Return {xMax, xMin, yMax, yMin}
    End Function

    Sub CreateDecals(LODCompDef As PartComponentDefinition)
        Dim faceOrder As Object = {"Right", "Bottom", "Left", "Top", "Back", "Front"}

        Dim nVM As Inventor.NameValueMap = g_inventorApplication.TransientObjects.CreateNameValueMap
        nVM.Add("TransparentBackground", True)

        For i = 0 To 5
            g_inventorApplication.CommandManager.ControlDefinitions("App" & faceOrder(i) & "ViewCmd").Execute()
            g_inventorApplication.ActiveView.SaveAsBitmapWithOptions("C:\Temp\" & faceOrder(i) & ".bmp", 0, 0, nVM)
            Dim activeBitmap As Drawing.Bitmap = New Drawing.Bitmap(CropBitmapImage("C:\Temp\" & faceOrder(i) & ".bmp"))
            activeBitmap.Save("C:\Temp\crop" & faceOrder(i) & ".bmp")

            Dim activeSketch As PlanarSketch = LODCompDef.Sketches.Add(LODCompDef.SurfaceBodies(1).Faces(i + 1), True)

            Dim sketchDimensions As Object = GetSketchDimensions(activeSketch)

            ' Insert image
            Dim insertPointVals As Object = GetImageInsertPoint(activeSketch)
            Dim insertPoint As Point2d = g_inventorApplication.TransientGeometry.CreatePoint2d(insertPointVals(0), insertPointVals(1))
            Dim activeSketchImage As SketchImage = activeSketch.SketchImages.Add("C:\Temp\crop" & faceOrder(i) & ".bmp", insertPoint)

            ' Scale image
            activeSketchImage.Width = sketchDimensions(0) - sketchDimensions(1)

            If faceOrder(i) = "Bottom" Then
                activeSketchImage.RotateRight()
                activeSketchImage.RotateRight()
            End If

            ' Create Decal
            LODCompDef.Features.DecalFeatures.Add(activeSketchImage, LODCompDef.SurfaceBodies(1).Faces(i + 1))

            activeBitmap.Dispose()
        Next
    End Sub
Message 7 of 10

Anonymous
Not applicable

This is just briliant if you want to keep large assemblies managable!

Many Thanks

 

How do you use it? Do you really replace the subassembly by the created simple part? Or do you switch Level Of Detail? Does the last option make it faster too or is it only when replacing the sub assembly by the simplified part?

0 Likes
Message 8 of 10

tdant
Collaborator
Collaborator

This automation creates a LOD Representation that you can activate from the parent assembly. For CPU and GPU purposes, it's the same as if you replaced the subassembly with the box-simplified part. The subassembly will remain in RAM, but that shouldn't have much affect on felt speed.

Message 9 of 10

Anonymous
Not applicable

@tdant 

Dear, What could be the reason that I get such a result. The images are not used on the correct sides...

 

Knipsel.JPG

 

 

0 Likes
Message 10 of 10

Anonymous
Not applicable

@tdant 

up

0 Likes