Stopping iLogic to add additional sketches and features

Stopping iLogic to add additional sketches and features

sam
Advocate Advocate
728 Views
6 Replies
Message 1 of 7

Stopping iLogic to add additional sketches and features

sam
Advocate
Advocate

Hi All, 

I have a iLogic code that adds a sketch and extrusion. If run again (or triggering automatically) it adds another sketch and gives errors. Is it possible to add a code that checks if the first sketch and extrusion is added and it doesn't run the rule. 

Thanks.

Regards, 

Sam

0 Likes
Accepted solutions (2)
729 Views
6 Replies
Replies (6)
Message 2 of 7

JamieVJohnson2
Collaborator
Collaborator

If checking for existing = false then

   create new

end if

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
Message 3 of 7

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @sam ,

 

There are likely several ways you can do this....  sometimes I just check for the feature and delete it first if it exists, and that way the code can run without issue... in this example the code looks for a feature named "cutout" and deletes it if found, and then after it's created it names it "cutout" so that it can be found by the rule next time:

 

' Create a new part document, using the default part template.
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
                
' a reference to the component definition.
Dim oCompDef As PartComponentDefinition
oCompDef = oDoc.ComponentDefinition

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

Try 
	oCompDef.Features.ExtrudeFeatures.Item("Cutout").Delete
Catch	
End Try

Dim oExtrude As ExtrudeFeature
oExtrude = oCompDef.Features.ExtrudeFeatures.Item(1)
                    
' Get the top face of the extrusion to use for creating the new sketch.
Dim oFrontFace As Face
oFrontFace = oExtrude.StartFaces.Item(1)

' Create a new sketch on this face, but use the method that allows you to
' control the orientation and orgin of the new sketch.
oSketch = oCompDef.Sketches.AddWithOrientation(oFrontFace, _
                oCompDef.WorkAxes.Item(1), True, True, oCompDef.WorkPoints(1))
                
' Determine where in sketch space the point (0.5,0.5,0) is.
Dim oCorner As Point2d
oCorner = oSketch.ModelToSketchSpace(oTransGeom.CreatePoint(0.5, 0.5, 0))

' Create the interior 3cm x 2cm rectangle for the pocket.
oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
            oCorner, oTransGeom.CreatePoint2d(oCorner.X + 3, oCorner.Y + 2))
            
' Create a profile.
oProfile = oSketch.Profiles.AddForSolid

' Create a pocket .25 cm deep.
oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kCutOperation)
Call oExtrudeDef.SetDistanceExtent(0.25, kNegativeExtentDirection)
oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
oExtrude.Name = "Cutout"

or you can look for the feature or sketch by name and exit the rule if it is found, again you'll want to have the code name the thing being looked for a predictable name as it is created so it can find it next time, in this example it's looking for a sketch called "Cutout_sketch" and if found the rule won't continue running.

 

' Create a new part document, using the default part template.
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
                
' a reference to the component definition.
Dim oCompDef As PartComponentDefinition
oCompDef = oDoc.ComponentDefinition

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

Try
    oTest = oCompDef.Sketches.Item("Cutout_Sketch")
    'found sketch so exit rule
    Return
Catch	 
End Try

Dim oExtrude As ExtrudeFeature
oExtrude = oCompDef.Features.ExtrudeFeatures.Item(1)
                    
' Get the top face of the extrusion to use for creating the new sketch.
Dim oFrontFace As Face
oFrontFace = oExtrude.StartFaces.Item(1)

' Create a new sketch on this face, but use the method that allows you to
' control the orientation and orgin of the new sketch.
oSketch = oCompDef.Sketches.AddWithOrientation(oFrontFace, _
                oCompDef.WorkAxes.Item(1), True, True, oCompDef.WorkPoints(1))
				
oSketch.Name = "Cutout_Sketch"				
                
' Determine where in sketch space the point (0.5,0.5,0) is.
Dim oCorner As Point2d
oCorner = oSketch.ModelToSketchSpace(oTransGeom.CreatePoint(0.5, 0.5, 0))

' Create the interior 3cm x 2cm rectangle for the pocket.
oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
            oCorner, oTransGeom.CreatePoint2d(oCorner.X + 3, oCorner.Y + 2))
            
' Create a profile.
oProfile = oSketch.Profiles.AddForSolid

' Create a pocket .25 cm deep.
oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kCutOperation)
Call oExtrudeDef.SetDistanceExtent(0.25, kNegativeExtentDirection)
oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
oExtrude.Name = "Cutout"

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 4 of 7

sam
Advocate
Advocate

Hi Curtis, 

Thanks for the prompt reply. Added as your advised and it has eliminated the sketch showing up with errors. Below is my code. Most of the code has been modified from your code in various post. Many more thanks for that as well. 

 

 	' Set a reference to the part component definition.
    ' This assumes that a part document is active.
    Dim oCompDef As PartComponentDefinition
    oCompDef = ThisApplication.ActiveDocument.ComponentDefinition
    
        
    ''Set a reference To the transient geometry Object.
    Dim oTransGeom As TransientGeometry
    oTransGeom = ThisApplication.TransientGeometry
    
   	'' Create face With the origin defined By
   	'' the center Point.  (The first WorkPoint In the Collection Is the center
    '' Point work Point.)  In this Case, no Sketch Geometry will be created
    '' since the final argument has been Left Out And it defaults To False.
    oFace = oCompDef.SurfaceBodies.Item(1).Faces.Item(3)
    oEdge = oFace.Edges.Item(1)
    oSketch = oCompDef.Sketches.AddWithOrientation(oFace, oEdge, True,True, oCompDef.WorkPoints.Item(1))
	
	Try
	    oTest = oCompDef.Sketches.Item("EndDetailsFeaureLeft")
	    'found sketch so exit rule
	    Return
	Catch	 
	End Try

     	
	Dim oXAxis2 As SketchEntity
   	oXAxis2 = oSketch.AddByProjectingEntity(oCompDef.WorkAxes.Item(3))
	
	Dim oXAxis3 As SketchEntity
	oXAxis3 = oSketch.AddByProjectingEntity(oCompDef.WorkPlanes.Item(4))
    
	' Draw a circle on the sketch.
    Dim oCircle1 As SketchCircle
    oCircle = oSketch.SketchCircles.AddByCenterRadius(oTransGeom.CreatePoint2d(15.1, ((LD+40)/10)), 1)
	Dim oCircle2 As SketchCircle
    oCircle = oSketch.SketchCircles.AddByCenterRadius(oTransGeom.CreatePoint2d(15.1, ((LD+160)/10)), 1)
	
	'Add hole dia hole 1
	Dim oDimension1 As DimensionConstraint = oSketch.DimensionConstraints.AddDiameter(oSketch.SketchCircles.Item(1), oTransGeom.CreatePoint2d(0, -1))
	
	'Add hole dia hole 2
	Dim oDimension2 As DimensionConstraint = oSketch.DimensionConstraints.AddDiameter(oSketch.SketchCircles.Item(2), oTransGeom.CreatePoint2d(0, 6.5))

	'Add Dimesnion between Projected line X asix and first circle
	Dim oDimension3 As DimensionConstraint = oSketch.DimensionConstraints.AddOffset(oSketch.SketchLines(1), oSketch.SketchPoints(1), ThisApplication.TransientGeometry.CreatePoint2d(3, 2), False)
	
	'Add Dimesnion between Projected line X asix & second circle
	Dim oDimension4 As DimensionConstraint = oSketch.DimensionConstraints.AddOffset(oSketch.SketchLines(1), oSketch.SketchPoints(2), ThisApplication.TransientGeometry.CreatePoint2d(2, 2), False)
	
	'Add Dimesnion between Projected line Z asix and first circle
	Dim oDimension5 As DimensionConstraint = oSketch.DimensionConstraints.AddOffset(oSketch.SketchLines(2), oSketch.SketchPoints(1), ThisApplication.TransientGeometry.CreatePoint2d(7.25, -2), False)
	
	'Add Dimesnion between Projected line Z asix & second circle
	Dim oDimension6 As DimensionConstraint = oSketch.DimensionConstraints.AddOffset(oSketch.SketchLines(2),oSketch.SketchPoints(2),ThisApplication.TransientGeometry.CreatePoint2d(7.25, -4), False)


  ' Draw a circle on the sketch.
    Dim oCircle3 As SketchCircle
    oCircle = oSketch.SketchCircles.AddByCenterRadius(oTransGeom.CreatePoint2d(-15.1, ((LD+40)/10)), 1)
	Dim oCircle4 As SketchCircle
    oCircle = oSketch.SketchCircles.AddByCenterRadius(oTransGeom.CreatePoint2d(-15.1, ((LD+160)/10)), 1)
	
	'Add hole dia hole 3
	Dim oDimension7 As DimensionConstraint = oSketch.DimensionConstraints.AddDiameter(oSketch.SketchCircles.Item(3), oTransGeom.CreatePoint2d(0, -1))
	
	'Add hole dia hole 4
	Dim oDimension8 As DimensionConstraint = oSketch.DimensionConstraints.AddDiameter(oSketch.SketchCircles.Item(4), oTransGeom.CreatePoint2d(0, 6.5))

	'Add Dimesnion between Projected line X asix and third circle
	Dim oDimension9 As DimensionConstraint = oSketch.DimensionConstraints.AddOffset(oSketch.SketchLines(1), oSketch.SketchPoints(3), ThisApplication.TransientGeometry.CreatePoint2d(-3, 2), False)
	
	'Add Dimesnion between Projected line X asix & second circle
	Dim oDimension10 As DimensionConstraint = oSketch.DimensionConstraints.AddOffset(oSketch.SketchLines(1), oSketch.SketchPoints(4), ThisApplication.TransientGeometry.CreatePoint2d(-2, 2), False)
	
	'Add Dimesnion between Projected line Z asix and first circle
	Dim oDimension11 As DimensionConstraint = oSketch.DimensionConstraints.AddOffset(oSketch.SketchLines(2), oSketch.SketchPoints(3), ThisApplication.TransientGeometry.CreatePoint2d(-7.25, -2), False)
	
	'Add Dimesnion between Projected line Z asix & second circle
	Dim oDimension12 As DimensionConstraint = oSketch.DimensionConstraints.AddOffset(oSketch.SketchLines(2),oSketch.SketchPoints(4),ThisApplication.TransientGeometry.CreatePoint2d(-7.25, -4), False)


  'Change the name.
    oSketch.Name = "EndDetailsFeaureLeft"
	
	
    ' Create a profile based on the circle
    Dim oProfile As Profile
    oProfile = oSketch.Profiles.AddForSolid

 	Dim oExtrudeDef As ExtrudeDefinition
    oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kCutOperation)
    ''Call oExtrudeDef.SetDistanceExtent(1, kNegativeExtentDirection)
	'Call oExtrudeDef.SetThroughAllExtent(ksymmetricExtentDirection)
	Call oExtrudeDef.SetDistanceExtent(2,kNegativeExtentDirection)
    Dim oExtrude As ExtrudeFeature
    oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
	' Change the name.
    oExtrude.Name = "EndDetailHolesLeft"
  

I have added a snapshot showing added sketch. 

 

Best regards, 

Sam

0 Likes
Message 5 of 7

sam
Advocate
Advocate

Hi @Curtis_Waguespack,

Checked with your code and actually with your code it doesn't generate new sketch if there is an existing sketch so there must be something wrong with my code. I will try to investigate line by line. 

 

Best regards, 

Sam

0 Likes
Message 6 of 7

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @sam ,

 

You might have already figured it out, but just in case I just reordered some things in your code and then it worked well.

 

' Set a reference to the part component definition.
' This assumes that a part document is active.
Dim oCompDef As PartComponentDefinition
oCompDef = ThisApplication.ActiveDocument.ComponentDefinition

    
''Set a reference To the transient geometry Object.
Dim oTransGeom As TransientGeometry
oTransGeom = ThisApplication.TransientGeometry

'' Create face With the origin defined By
'' the center Point.  (The first WorkPoint In the Collection Is the center
'' Point work Point.)  In this Case, no Sketch Geometry will be created
'' since the final argument has been Left Out And it defaults To False.
oFace = oCompDef.SurfaceBodies.Item(1).Faces.Item(3)
oEdge = oFace.Edges.Item(1)

'check for existing sketch
Try
    oTest = oCompDef.Sketches.Item("EndDetailsFeaureLeft")
    'found sketch so exit rule
    Return
Catch	 
End Try

'add sketch
 oSketch = oCompDef.Sketches.AddWithOrientation _
 (oFace, oEdge, True, True, oCompDef.WorkPoints.Item(1))
	 
'Change the name.
oSketch.Name = "EndDetailsFeaureLeft"
	 
Dim oXAxis2 As SketchEntity
oXAxis2 = oSketch.AddByProjectingEntity(oCompDef.WorkAxes.Item(3))

'the rest of the code here......

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 7 of 7

sam
Advocate
Advocate

@Curtis_Waguespack Hi Curtis, I tried but couldn't figure out what was wrong. I have seen now that oSketch was actually being created before error check which you have moved after. Brilliant. Thank you so much. Have a good day Sir. 

0 Likes