Create irregular sketch on a part of an assembly using API

nishantkumat5921
Enthusiast
Enthusiast

Create irregular sketch on a part of an assembly using API

nishantkumat5921
Enthusiast
Enthusiast

Hello Everyone,

 

I am trying to make the below shape on a assembly occurrence and then extrude cut the portion. Since the shape is not common I require some help on making the sketch in API using the easiest method.

cutout.PNG

Thanks in advance.

0 Likes
Reply
Accepted solutions (1)
590 Views
4 Replies
Replies (4)

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @nishantkumat5921 

This one was more difficult than I thought it would be. That's because functions such as trim etc. in sketches aren't exposed through the API. I'd recommend that you use a sketchblock or an iFeature for your purpose, but if you want to sketch it through API I have an example for you here.

I just wrote the code for the sketch so this example is in part environment. Just open a new part and run this iLogic code to see it work. The code asks you to input some dimensions and then pick a workplane.

 

Dim Diameter As Double = CDblAny(InputBox("Diameter: ", "Diameter", "30"))
Dim crossGap As Double = CDblAny(InputBox("CrossGap: ", "Cross gap", "4"))
Dim crossHeight As Double = CDblAny(InputBox("CrossHeight: ", "Cross height", "35"))


If crossHeight <= Diameter Then
	MsgBox("Crossheight must be greater than diameter")
	Exit Sub
End If
If crossGap >= Diameter Then
	MsgBox("Crossgap must be smaller than diameter")
	Exit Sub
End If

Dim oDoc As PartDocument = ThisDoc.Document
Dim UoM As UnitsOfMeasure = oDoc.UnitsOfMeasure

Dim dia As Double = UoM.ConvertUnits(Diameter, UoM.LengthUnits, UnitsTypeEnum.kDatabaseLengthUnits)
Dim cg As Double = UoM.ConvertUnits(crossGap, UoM.LengthUnits, UnitsTypeEnum.kDatabaseLengthUnits)
Dim ch As Double = UoM.ConvertUnits(crossHeight, UoM.LengthUnits, UnitsTypeEnum.kDatabaseLengthUnits)


Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim oPlane As WorkPlane = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kWorkPlaneFilter, "Pick a workplane")
Dim oSketch As PlanarSketch = oDef.Sketches.Add(oPlane)
Dim oSketchCenterPoint As SketchPoint = oSketch.AddByProjectingEntity(oDef.WorkPoints("Center Point"))
Dim cp As Point2d = oSketchCenterPoint.Geometry
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oCircle As Circle2d = oTG.CreateCircle2d(cp, dia / 2)


Dim rightLine As Line2d = oTG.CreateLine2d(oTG.CreatePoint2d(cp.X + cg/2, 0), oTG.CreateUnitVector2d(0, 1))
Dim leftLine As Line2d = oTG.CreateLine2d(oTG.CreatePoint2d(cp.X - cg/2, 0), oTG.CreateUnitVector2d(0, 1))
Dim upperLine As Line2d = oTG.CreateLine2d(oTG.CreatePoint2d(0, cp.Y + cg/2), oTG.CreateUnitVector2d(1, 0))
Dim lowerLine As Line2d = oTG.CreateLine2d(oTG.CreatePoint2d(0, cp.Y - cg/2), oTG.CreateUnitVector2d(1, 0))
Dim rightIntersect As ObjectsEnumerator = rightLine.IntersectWithCurve(oCircle)
Dim leftIntersect As ObjectsEnumerator = leftLine.IntersectWithCurve(oCircle)
Dim upperIntersect As ObjectsEnumerator = upperLine.IntersectWithCurve(oCircle)
Dim lowerIntersect As ObjectsEnumerator = lowerLine.IntersectWithCurve(oCircle)
Dim p1 As Point2d
Dim p2 As Point2d
Dim p3 As Point2d
Dim p4 As Point2d
Dim p5 As Point2d
Dim p6 As Point2d
Dim p7 As Point2d
Dim p8 As Point2d
If rightIntersect(1).Y > rightIntersect(2).Y
	p1 = rightIntersect(1)
	p4 = rightIntersect(2)
Else
	p4 = rightIntersect(1)
	p1 = rightIntersect(2)
End If
Dim sp1 As SketchPoint = oSketch.SketchPoints.Add(p1, False)
Dim sp4 As SketchPoint = oSketch.SketchPoints.Add(p4, False)
If leftIntersect(1).Y > leftIntersect(2).Y
	p8 = leftIntersect(1)
	p5 = leftIntersect(2)
Else
	p5 = leftIntersect(1)
	p8 = leftIntersect(2)
End If
Dim sp5 As SketchPoint = oSketch.SketchPoints.Add(p5, False)
Dim sp8 As SketchPoint = oSketch.SketchPoints.Add(p8, False)
If upperIntersect(1).X > upperIntersect(2).X
	p2 = upperIntersect(1)
	p7 = upperIntersect(2)
Else
	p7 = upperIntersect(1)
	p2 = upperIntersect(2)
End If
Dim sp7 As SketchPoint = oSketch.SketchPoints.Add(p7, False)
Dim sp2 As SketchPoint = oSketch.SketchPoints.Add(p2, False)
If lowerIntersect(1).X > lowerIntersect(2).X
	p3 = lowerIntersect(1)
	p6 = lowerIntersect(2)
Else
	p6 = lowerIntersect(1)
	p3 = lowerIntersect(2)
End If
Dim sp6 As SketchPoint = oSketch.SketchPoints.Add(p6, False)
Dim sp3 As SketchPoint = oSketch.SketchPoints.Add(p3, False)

Dim a1 As SketchArc = oSketch.SketchArcs.AddByCenterStartEndPoint(oSketchCenterPoint, sp1, sp2, False)
Dim a2 As SketchArc = oSketch.SketchArcs.AddByCenterStartEndPoint(oSketchCenterPoint, sp3, sp4, False)
Dim a3 As SketchArc = oSketch.SketchArcs.AddByCenterStartEndPoint(oSketchCenterPoint, sp5, sp6, False)
Dim a4 As SketchArc = oSketch.SketchArcs.AddByCenterStartEndPoint(oSketchCenterPoint, sp7, sp8, False)

oSketch.GeometricConstraints.AddCoincident(oSketchCenterPoint, a1.CenterSketchPoint)
oSketch.DimensionConstraints.AddDiameter(a1, cp)

oSketch.GeometricConstraints.AddCoincident(a1.CenterSketchPoint, a2.CenterSketchPoint)
oSketch.GeometricConstraints.AddCoincident(a1.CenterSketchPoint, a3.CenterSketchPoint)
oSketch.GeometricConstraints.AddCoincident(a1.CenterSketchPoint, a4.CenterSketchPoint)
oSketch.GeometricConstraints.AddEqualRadius(a1, a2)
oSketch.GeometricConstraints.AddEqualRadius(a1, a3)
oSketch.GeometricConstraints.AddEqualRadius(a1, a4)

Dim p1p4 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp1, sp4)
p1p4.Construction = True
Dim p2p7 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp2, sp7)
p2p7.Construction = True
Dim p3p6 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp3, sp6)
p3p6.Construction = True
Dim p5p8 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp5, sp8)
p5p8.Construction = True

oSketch.GeometricConstraints.AddParallel(p1p4, p5p8)
oSketch.GeometricConstraints.AddParallel(p2p7, p3p6)
oSketch.GeometricConstraints.AddEqualLength(p1p4, p5p8)
oSketch.GeometricConstraints.AddEqualLength(p1p4, p2p7)
oSketch.GeometricConstraints.AddEqualLength(p1p4, p3p6)
oSketch.GeometricConstraints.AddPerpendicular(p1p4, p2p7)

Dim cgDim As OffsetDimConstraint = oSketch.DimensionConstraints.AddOffset(p1p4, p5p8, cp, False)
cgDim.Parameter.Value = cg

Dim r1 As SketchPoint = oSketch.SketchPoints.Add(oTG.CreatePoint2d(p8.X, p8.Y + 5), False)
Dim r2 As SketchPoint = oSketch.SketchPoints.Add(oTG.CreatePoint2d(p1.X, p1.Y + 5), False)
Dim l1 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp8, r1)
Dim l2 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp1, r2)
oSketch.GeometricConstraints.AddEqualLength(l1, l2)
Dim e1 As SketchLine = oSketch.SketchLines.AddByTwoPoints(r1, r2)
oSketch.GeometricConstraints.AddCollinear(p5p8, l1)
oSketch.GeometricConstraints.AddCollinear(p1p4, l2)

Dim r3 As SketchPoint = oSketch.SketchPoints.Add(oTG.CreatePoint2d(p5.X, p5.Y - 5), False)
Dim r4 As SketchPoint = oSketch.SketchPoints.Add(oTG.CreatePoint2d(p4.X, p4.Y - 5), False)
Dim l3 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp5, r3)
Dim l4 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp4, r4)
oSketch.GeometricConstraints.AddEqualLength(l3, l4)
Dim e2 As SketchLine = oSketch.SketchLines.AddByTwoPoints(r3, r4)
oSketch.GeometricConstraints.AddCollinear(p5p8, l3)
oSketch.GeometricConstraints.AddCollinear(p1p4, l4)

Dim r5 As SketchPoint = oSketch.SketchPoints.Add(oTG.CreatePoint2d(p2.X + 5, p2.Y), False)
Dim r6 As SketchPoint = oSketch.SketchPoints.Add(oTG.CreatePoint2d(p3.X + 5, p3.Y), False)
Dim l5 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp2, r5)
Dim l6 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp3, r6)
oSketch.GeometricConstraints.AddEqualLength(l5, l6)
Dim e3 As SketchLine = oSketch.SketchLines.AddByTwoPoints(r5, r6)
oSketch.GeometricConstraints.AddCollinear(p2p7, l5)
oSketch.GeometricConstraints.AddCollinear(p3p6, l6)

Dim r7 As SketchPoint = oSketch.SketchPoints.Add(oTG.CreatePoint2d(p7.X - 5, p7.Y), False)
Dim r8 As SketchPoint = oSketch.SketchPoints.Add(oTG.CreatePoint2d(p6.X - 5, p6.Y), False)
Dim l7 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp7, r7)
Dim l8 As SketchLine = oSketch.SketchLines.AddByTwoPoints(sp6, r8)
oSketch.GeometricConstraints.AddEqualLength(l7, l8)
Dim e4 As SketchLine = oSketch.SketchLines.AddByTwoPoints(r7, r8)
oSketch.GeometricConstraints.AddCollinear(p2p7, l7)
oSketch.GeometricConstraints.AddCollinear(p3p6, l8)



oSketch.GeometricConstraints.AddEqualLength(l1, l3)
oSketch.GeometricConstraints.AddEqualLength(l1, l5)
oSketch.GeometricConstraints.AddEqualLength(l1, l7)
Dim chDim As OffsetDimConstraint = oSketch.DimensionConstraints.AddOffset(e1, e2, cp, False)
chDim.Parameter.Value = ch

oSketch.GeometricConstraints.AddHorizontal(p2p7) 'Uncomment this if shape should be rotatable

oDoc.Update

nishantkumat5921
Enthusiast
Enthusiast

Thanks for the response @JhoelForshav.

 

I had an idea of drawing a circle and two rectangles and my moto was to extrude cut I will select all and then use extrude. Can u tell me the feasibility of this using C# and not ilogic. As this method will not require any trim or mirror option to be used.

0 Likes

JhoelForshav
Mentor
Mentor

@nishantkumat5921 

You can use C#, iLogic, VB.NET... whatever you feel most comfortable with. It's the same API so one is not better than the other. With iLogic you have some simplified functions/methods and some extra interfaces but it's all the same really. You can sketch the profile with the method I sent you or you can just draw a circle and two rectangles and add all profiles in the sketch to your extrude. Or just use a sketchblock for it and import that. That way your code doesn't have to do any sketching at all. Or create an iFeature to extrude this profile. I've done a lot of automation in Inventor and sketching by code should definitely be considered a last resort if you ask me.

0 Likes

nishantkumat5921
Enthusiast
Enthusiast

Is it possible for you to share the import ifeature code as it reduces the work of sketching every time. Meanwhile ill try the above code as well.

0 Likes