VBA creating sketch on named Face

VBA creating sketch on named Face

sam
Advocate Advocate
2,379 Views
15 Replies
Message 1 of 16

VBA creating sketch on named Face

sam
Advocate
Advocate

Hi All, 

Hope everyone is staying safe. I am trying to access a named face in VBA and then create a sketch on that face. I was able to locate the names faces in object brown inside VBA but whenever I try to call it my code gives errors. Is it because VBA doesn't support iLogic functionality? Any help will be appreciated. 

 

Best regards, 

sam

 

Inventor Professional 2019

0 Likes
Accepted solutions (2)
2,380 Views
15 Replies
Replies (15)
Message 2 of 16

brendon.serrano
Enthusiast
Enthusiast

I know that VBA and iLogic are similar, and I have a bit of code from iLogic I used to create a Bar Extrusion. I did this by creating the sketch in iLogic, but I'm sure if you just call the name to be the active object you will be able to do it. (assuming that it is the same in VBA)

 

' Create a new sketch.
Dim sketch As Inventor.PlanarSketch
sketch = oDef.Sketches.Add(xyPlane, True)
sketch.Name = "Bar_Sketch"

ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches("Bar_Sketch").Edit


InventorVb.DocumentUpdate()


' Check to make sure a sketch is open.
If Not TypeOf ThisApplication.ActiveEditObject Is PlanarSketch Then
MessageBox.Show("A sketch must be active.", "iLogic")
Return
End If

'set a reference to the active sketch.
Dim oSketch As PlanarSketch
oSketch = ThisApplication.ActiveEditObject

'set a reference to the transient geometry collection.
Dim oTransGeom As TransientGeometry
oTransGeom = ThisApplication.TransientGeometry

' Create a rectangle 

width = Parameter("BAR_W") 'Trigger
height = Parameter("BAR_H") 'Trigger
length = Parameter("BAR_L") 'Trigger

Dim oRectangleLines As SketchEntitiesEnumerator
oRectangleLines = oSketch.SketchLines.AddAsTwoPointCenteredRectangle( oTransGeom.CreatePoint2d(0, 0),oTransGeom.CreatePoint2d(width*2.54/2, height*2.54/2))

Dim oSketchLine As Inventor.SketchLine
Dim oDim As DimensionConstraint

oSketchLine = oRectangleLines.Item(1) 

oDim = oSketch.DimensionConstraints.AddTwoPointDistance _
	(oSketchLine.StartSketchPoint, oSketchLine.EndSketchPoint, _
	DimensionOrientationEnum.kHorizontalDim, _
	oTransGeom.CreatePoint2d(0, -(width/2 )))

oDim.Parameter.Expression = "BAR_W"

oSketchLine = oRectangleLines.Item(2)

oDim = oSketch.DimensionConstraints.AddTwoPointDistance _
	(oSketchLine.StartSketchPoint, oSketchLine.EndSketchPoint, _
	DimensionOrientationEnum.kVerticalDim, _
	oTransGeom.CreatePoint2d(height+2, 0))

oDim.Parameter.Expression = "BAR_H"

ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches("Bar_Sketch").ExitEdit

Hope this helps!

Message 3 of 16

DRoam
Mentor
Mentor

Please share your code and the errors you're getting. It would be futile to guess at what's going wrong without either of these.

 

Also, may I ask why you're using VBA rather than iLogic?

Message 4 of 16

WCrihfield
Mentor
Mentor

If you posted the code here, we might be able to troubleshoot it for you.

I'm not sure but, I believe you're right about that specific Type not being recognized in VBA.

I think NamedEntities is a special Class, that is pre-set for iLogic, but not in VBA.

You may have to include a Reference or define the Class.

You'll notice in iLogic's Rule Editor, it is classified as "Autodesk.iLogic.Interfaces.NamedEntities".

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 16

sam
Advocate
Advocate

@DRoam thanks for the reply. 

I have been practicing iLogic for few weeks now and I have done quite few rules (a lot to courtesy of your your help and help of @WCrihfield  and other senior members of forum)   to automate my work and I have been successful with that. 

 

Problem is when I try to put all together in form, iLogic forms are really basic. Our IT head office has refused to install VS.Net and I thought I will just play around with VBA and see how it goes. Through object browser I can see those face names in VBA editor but it is really deep in nodes. below is the code. 

 

 

Public Sub AddSketch()
    Dim oCompDef As PartComponentDefinition
    Set oCompDef = ThisApplication.ActiveDocument.ComponentDefinition
    Dim oFace As Face
    Set oFace = oCompDef.AttributeSets.Item(1).Name("nameFace")
    Dim oSketch As PlanarSketch
    Set oSketch = oCompDef.Sketches.Add(oFace, True)
    oSketch.Name = "My New Sketch"
End Sub

 

 

The error it is showing is:

 
 

Thanks in advance, 

Regards, 

Sam

0 Likes
Message 6 of 16

sam
Advocate
Advocate

@WCrihfield thanks for the reply and hope you are doing alright.  Face.item(index) numbers are changing as we are progressing adding more and more features through code or we re compute in any way. What is other fixed name or number calling entity that I could use in VBA?

0 Likes
Message 7 of 16

sam
Advocate
Advocate

Capture.PNG

 

sorry picture was not uploaded previously. 

0 Likes
Message 8 of 16

JelteDeJong
Mentor
Mentor
Accepted solution

named faces are attributes under the hood. try this vba code.  just change the namedFace vaiable to what ever you named the face.

Dim namedFace As String
namedFace = "testFace"

Dim doc As PartDocument
Set doc = ThisApplication.ActiveDocument

Dim face As face
Set face = doc.AttributeManager.FindObjects(, , namedFace)(1)

Dim sketch As PlanarSketch
Set sketch = doc.ComponentDefinition.Sketches.Add(face)

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 9 of 16

sam
Advocate
Advocate

@JelteDeJong wrote:

named faces are attributes under the hood. try this vba code.  just change the namedFace vaiable to what ever you named the face.

 

Dim namedFace As String
namedFace = "testFace"

Dim doc As PartDocument
Set doc = ThisApplication.ActiveDocument

Dim face As face
Set face = doc.AttributeManager.FindObjects(, , namedFace)(1)

Dim sketch As PlanarSketch
Set sketch = doc.ComponentDefinition.Sketches.Add(face)

 

 


Hi, 

thanks for the reply. 

I tried this and got the following error. 

 

 

Public Sub AddSketch()

Dim WebIntFace01 As String
WebIntFace01 = "testFace"

Dim doc As PartDocument
Set doc = ThisApplication.ActiveDocument

Dim face As face
Set face = doc.AttributeManager.FindObjects(, , WebIntFace01)(1)

Dim sketch As PlanarSketch
Set sketch = doc.ComponentDefinition.Sketches.Add(face)

End Sub

 

 

Error: Run-time error '5':

Invalid Procedure call or argument. 

 

Also before testing I knew something was unusual about the code. What purpose we are achieving in second line by declaring WebIntFace01 = "testFace". 

Did I do something wrong here?

 

Best regards, 

Sam

0 Likes
Message 10 of 16

sam
Advocate
Advocate

@JelteDeJong wrote:

named faces are attributes under the hood. try this vba code.  just change the namedFace vaiable to what ever you named the face.

 

Dim namedFace As String
namedFace = "testFace"

Dim doc As PartDocument
Set doc = ThisApplication.ActiveDocument

Dim face As face
Set face = doc.AttributeManager.FindObjects(, , namedFace)(1)

Dim sketch As PlanarSketch
Set sketch = doc.ComponentDefinition.Sketches.Add(face)

 

 


I played around and when I changed it liked below, it worked. 

 

Dim testFace As String
testFace = "WebIntFace01"

 

I am going to try to draw something and then add couple of more features and I might be bugging you again for more help. Thanks for the help so far. 

 

best regards, 

Sam

0 Likes
Message 11 of 16

sam
Advocate
Advocate

Hi @JelteDeJong  again, 

 

As I explained earlier, I managed to create sketch by changing your recommended code as below but I can't add simple sketch entities on it. 

Public Sub AddSketch()

Dim testFace As String
testFace = "WebIntFace01"

Dim doc As PartDocument
Set doc = ThisApplication.ActiveDocument

Dim face As face
Set face = doc.AttributeManager.FindObjects(, , testFace)(1)

Dim sketch As PlanarSketch
Set sketch = doc.ComponentDefinition.Sketches.Add(face)

Dim oTransGeom As TransientGeometry
Set oTransGeom = ThisApplication.TransientGeometry
   
Dim oCircle As SketchCircle
Set oCircle = oSketch1.SketchCircles.AddByCenterRadius(oTransGeom.CreatePoint2d(0, 0), 2)
    
End Sub

 

Error it gives is: Run-time error '424':

Object required. 

 

Any idea what am I doing wrong here. 

 

Best regards, 

Sam

0 Likes
Message 12 of 16

JelteDeJong
Mentor
Mentor
Accepted solution

I guess a typo. try to replace the line:

 

Set oCircle = oSketch1.SketchCircles.AddByCenterRadius(oTransGeom.CreatePoint2d(0, 0), 2)

 

with:

 

Set oCircle = sketch.SketchCircles.AddByCenterRadius(oTransGeom.CreatePoint2d(0, 0), 2)

 

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 13 of 16

sam
Advocate
Advocate

@JelteDeJong 

 

Hi I figured that out too last night. It was actually not a typo rather  a mistake because I was copy pasting from bunch of ilogic rules. Thanks for the taking time to read through it and pointing out. Really appreciate your help. 

 

Best regards, 

Sam

0 Likes
Message 14 of 16

sam
Advocate
Advocate

@brendon.serrano That is really interesting code. I am trying to learn as much as possible and I find it fascinating how same thing can be done by so many different ways. Would you mind explaining couple of points?

when you are calling parameters what does those lines of code actually do? do they update the parameters at that point or that make sure that always updated values are accessed from those parameters?

 

apologies if you find this question very basic. I will also try to find out myself by replicating those parameters. 

 

stay safe and take care. 

thanks and regards, 

sam

0 Likes
Message 15 of 16

brendon.serrano
Enthusiast
Enthusiast

@sam No Problem! I have only been using iLogic for a couple months now, but I tend to enjoy coding. My code is a mixture of what I could find and what I could make work. I initially had the parameters in the code because I was using it as an internal rule. When an internal rule has a parameter in it called by only using its name (eg. "MyParam", FYI: You can only do this in internal rules), when this parameter is changed or edited the rule will trigger. I created a form to extrude a bar and I put those parameters in the form for the user to fill out. This way when they could simply fill out a length width and height on the form to quickly create a bar (I did this to make people draw off the origin as much as possible). Anyway, I scrapped the parameters as a trigger by calling them with "Parameter("MyParam")" and added the iLogic code as a button on the end of the form. My final product with this code was using a macro to create a button on the ribbon that would prompt the form and the user inputs desired values and hits create. A bar is then created on the origin planes with the input parameters as its dimensions. Still doesn't work perfectly. I have a bug where when the bar is already created and the parameters are changed, it will no longer be on the origin. I just haven't bothered fixing it yet. 

 

I moved all of my code over to external rules for ease of use on the network, and for legacy documents. Here is my full code and some screen shots of the process. Hope this helps, let me know if you have any other questions. 

 

Dim oDoc = ThisDoc.Document

If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
    ' Set a reference to the active sketch.    
    Dim OpenSketch As Sketch = ThisApplication.ActiveEditObject
    OpenSketch.ExitEdit
End If

Dim oDef As PartComponentDefinition
oDef = oDoc.ComponentDefinition


' Get the X-Y work plane.
Dim xyPlane As WorkPlane
xyPlane = oDef.WorkPlanes.Item("XY Plane")

Try
	ExtrusionExists = ThisApplication.ActiveEditDocument.ComponentDefinition.Features("Bar Extrusion")
	ThisApplication.ActiveView.Fit

	Catch
		Try
			SketchExists = ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches("Bar_Sketch")
			SketchExists.delete
			Catch
		End Try 


' Create a new sketch.
Dim sketch As Inventor.PlanarSketch
sketch = oDef.Sketches.Add(xyPlane, True)
sketch.Name = "Bar_Sketch"

ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches("Bar_Sketch").Edit


InventorVb.DocumentUpdate()


' Check to make sure a sketch is open.
If Not TypeOf ThisApplication.ActiveEditObject Is PlanarSketch Then
MessageBox.Show("A sketch must be active.", "iLogic")
Return
End If

'set a reference to the active sketch.
Dim oSketch As PlanarSketch
oSketch = ThisApplication.ActiveEditObject

'set a reference to the transient geometry collection.
Dim oTransGeom As TransientGeometry
oTransGeom = ThisApplication.TransientGeometry

' Create a rectangle 

width = Parameter("BAR_W") 'Trigger
height = Parameter("BAR_H") 'Trigger
length = Parameter("BAR_L") 'Trigger

Dim oRectangleLines As SketchEntitiesEnumerator
oRectangleLines = oSketch.SketchLines.AddAsTwoPointCenteredRectangle( oTransGeom.CreatePoint2d(0, 0),oTransGeom.CreatePoint2d(width*2.54/2, height*2.54/2))

Dim oSketchLine As Inventor.SketchLine
Dim oDim As DimensionConstraint

oSketchLine = oRectangleLines.Item(1) 

oDim = oSketch.DimensionConstraints.AddTwoPointDistance _
	(oSketchLine.StartSketchPoint, oSketchLine.EndSketchPoint, _
	DimensionOrientationEnum.kHorizontalDim, _
	oTransGeom.CreatePoint2d(0, -(width/2 )))

oDim.Parameter.Expression = "BAR_W"

oSketchLine = oRectangleLines.Item(2)

oDim = oSketch.DimensionConstraints.AddTwoPointDistance _
	(oSketchLine.StartSketchPoint, oSketchLine.EndSketchPoint, _
	DimensionOrientationEnum.kVerticalDim, _
	oTransGeom.CreatePoint2d(height+2, 0))

oDim.Parameter.Expression = "BAR_H"

ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches("Bar_Sketch").ExitEdit


Dim extrudeProfile As Profile = sketch.Profiles.AddForSolid 
Dim oExtFeature As ExtrudeFeature
oExtFeature = oDoc.ComponentDefinition.Features.ExtrudeFeatures.AddByDistanceExtent (extrudeProfile, "BAR_L", kSymmetricExtentDirection, kJoinOperation)
oExtFeature.Name = "Bar Extrusion"

ThisApplication.ActiveView.Fit
iLogicVb.UpdateWhenDone = True


End Try

 Extrude Bar Button and Form.png

Message 16 of 16

sam
Advocate
Advocate

HI @brendon.serrano , 

Hoping that you are staying safe. 
Thanks for explaining. No doubt about coding becoming fun when you start understanding it. At the moment I understand it all but may be later when I might ask you more questions. Thanks again. take care. 

 

regards, 

Sam

0 Likes