Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Custom TubeMaker VBA

1 REPLY 1
Reply
Message 1 of 2
bigslim
293 Views, 1 Reply

Custom TubeMaker VBA

This is from a sample code tutorial I found on the internet. Not being a programmer (yet), it's all I could do to get this tiny bit of VBA code working properly. I'm trying to better understand the basic workflow in using VBA to edit/write new code for inventor, and I figured this would be a good place to start. 

The .ivb code I have here opens a UserForm and prompts the user for 3 inputs: InnerDiameter, Outer Diameter, and Length. When the user clicks the "CreateButton", the UserForm closes out, launches inventor, and creates a new part.

I think I understand the little piece of code I painstakingly copied into my VBA editor, but I would like to add to/edit it. This is where I'm hoping some of you more experienced guys can step in and offer little snippets of code I can use to make this better. 

A few simple things I've been trying to do with it so far is change the units to mm instead of cm, and make the program stay open so that I can add more buttons to the userform. Ultimately, I want to add a button to continue my pipe operation, possible adding a sweep function that will create bends. All ideas welcome. My real goal here is learning to write code in the .NET/COM environment.

 

Here is the code that was used to create the straight portion of tubing shown in the .jpg.

 


Option Explicit

Private Sub CreateButton_Click()
'Define variables
Dim CenterRadiusO As Double
Dim CenterRadiusI As Double
Dim DistanceExtent As Double

'Set variable from values in edit boxes
CenterRadiusO = OuterDiameter / 2
CenterRadiusI = InnerDiameter / 2
DistanceExtent = PipeLength

'error message if InnerDiameter is greater than OuterDiameter
If InnerDiameter > OuterDiameter Then
CreateButton.Enabled = False
MsgBox ("What kind of tube/pipe do you think I am? I may be hollow inside, but I'm not invisible. The Inner Diameter must be smaller than the Outer Diameter.")
Exit Sub
End If

'Create new part document
Dim oDoc As PartDocument
Set oDoc = ThisApplication.Documents.Add(kPartDocumentObject)
' Get component definition from part document
Dim oCompDef As ComponentDefinition
Set oCompDef = oDoc.ComponentDefinition

' create a new sketch on the X-Y work plane.
Dim Sketch1 As PlanarSketch
Set Sketch1 = oCompDef.Sketches.Add(oCompDef.WorkPlanes.Item(3))

' Set a reference to the transient geometry object.
Dim oTransGeom As TransientGeometry
Set oTransGeom = ThisApplication.TransientGeometry

' Draw a circle on the sketch.
Dim OuterCircle As SketchCircle
Set OuterCircle = Sketch1.SketchCircles.AddByCenterRadius(oTransGeom.CreatePoint2d(0, 0), CenterRadiusO)
Dim InnerCircle As SketchCircle
Set InnerCircle = Sketch1.SketchCircles.AddByCenterRadius(oTransGeom.CreatePoint2d(0, 0), CenterRadiusI)

'Create a profile.
Dim Profile As Profile
Set Profile = Sketch1.Profiles.AddForSolid

' Create a solid extrusion.
Dim Extrusion1 As ExtrudeFeature
Set Extrusion1 = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent(Profile, DistanceExtent, kSymmetricExtentDirection, kJoinOperation)

' fit the view
ThisApplication.ActiveView.Fit

'close dialog
End
End Sub

Private Sub ExitButton_Click()
End
End Sub

Private Sub UserForm_Click()

End Sub

 

tubemaker.jpg

 

I want my program to leave the PipeMaker UserForm open until I'm done creating my Piperun. Please don't chime in telling me someone already did this. Great! Wonderful! Grand! No me Importa! I want to learn how to program, and I'm already familiar with this chunk of code, so I want to use it as my learning module. There's got to be half a dozen different ways to do this, so like I said earlier, I'm open to better ideas than creating a sweep function to continue my operation.

Tags (1)
1 REPLY 1
Message 2 of 2
bigslim
in reply to: bigslim

Progress update: I was able to keep the UserForm open by deleting "End" command at the end of my first sub-routine, simple enough, but I can't toggle back to my model space and view my part until I close the UserForm.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report