Creating a Polygon Sketch in VBA

Creating a Polygon Sketch in VBA

austen_27_t
Contributor Contributor
1,625 Views
7 Replies
Message 1 of 8

Creating a Polygon Sketch in VBA

austen_27_t
Contributor
Contributor

I need to create a sketch of a polygon using VBA but I do not know the code to make the happen.

I would appreciate any help!

 

This is the code i have i changed the code that i had for a circle and tried to make it work for the polygon the problem should be in the area of the code that is double spaced out.

 

Public Sub hex()
Dim doc As PartDocument
Set doc = ThisApplication.ActiveDocument

Dim partD As PartComponentDefinition
Set partD = doc.ComponentDefinition

Dim dim1, dim2

dim1 = InputBox("Enter Radius of Cylinder", "Radius")
dim2 = InputBox("Enter Length of Cylinder", "Length")

Dim sketch As PlanarSketch
Set sketch = partD.Sketches.Add(partD.WorkPlanes.Item(3))

Dim tg As TransientGeometry
Set tg = ThisApplication.TransientGeometry

 


Dim hex As SketchPolygon
Set hex = sketch.SketchPolygons.AddByCenterRadius(ThisApplication.TransientGeometry.CreatePoint2d(0, 0), dim1)

 


'Creates a profile.
Dim Pfile As Profile
Set Pfile = sketch.Profiles.AddForSolid

'Create an extrusion.
Dim extrudeDef As ExtrudeDefinition
Set extrudeDef = partD.Features.ExtrudeFeatures.CreateExtrudeDefinition(Pfile, kJoinOperation)

Call extrudeDef.SetDistanceExtent(dim2, kNegativeExtentDirection)
Dim extrude As ExtrudeFeature
Set extrude = partD.Features.ExtrudeFeatures.Add(extrudeDef)

End Sub

0 Likes
1,626 Views
7 Replies
Replies (7)
Message 2 of 8

arron.craig
Collaborator
Collaborator

I think you would need to sketch one from lines similar to the API sample

 

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2018/ENU/Inventor-API/files/Sk...

Message 3 of 8

dgreatice
Collaborator
Collaborator

identify number of side?

identify inscribed or circumscribed?

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
Message 4 of 8

ravikmb5
Collaborator
Collaborator

Try This

 

but i was not able to figure it out

to Dimension the Sketch Entities of polygon

 

 

Sub Main()


	' Defining This Document
	Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
	'Definining a Component Definition
	Dim oPartDef As PartComponentDefinition = oPartDoc.ComponentDefinition
	Dim oParams As Parameters
	oParams = oPartDef.Parameters
	Dim oUserParams As UserParameters
	oUserParams = oParams.UserParameters
	

	'[ Create's a Profile Sketch
	Dim sketch1 As PlanarSketch
	sketch1 = oPartDef.Sketches.Add(oPartDef.WorkPlanes.Item(3))
	sketch1.Name = "Profile Extrude Sketch"
	'Collecting Transient Geometry
	Dim tg As TransientGeometry
	tg = ThisApplication.TransientGeometry





	'''CentreOrigin Projected
	 Dim oProjectionPoint As SketchPoint
	oProjectionPoint = sketch1.AddByProjectingEntity(oPartDef.WorkPoints.Item(1))

	'''PolyGon Sketch
		Dim hex As SketchEntitiesEnumerator
		hex = sketch1.SketchLines.AddAsPolygon(5,oProjectionPoint,tg.CreatePoint2d(-0.5,-0.6882),True)
		
	''' Construcyion Circle	
		Dim oCir As SketchCircle
		oCir = sketch1.SketchCircles.AddByCenterRadius(oProjectionPoint, 1.3764 / 2)
		oCir.Construction = True
		
		
		'' Constrained Circle tangent to Lines of Polygon
		Call sketch1.GeometricConstraints.AddTangent(hex.Item(1), oCir)
		Call sketch1.GeometricConstraints.AddTangent(hex.Item(2),oCir)
		Call sketch1.GeometricConstraints.AddTangent(hex.Item(3),oCir)
		
		
		''' Circle Centre Point to Origin Point Merge
		Call oCir.CenterSketchPoint.Merge(oProjectionPoint)
		
		''' Adding Horizontal Constraint to one of the Lines of Polygon
		Call sketch1.GeometricConstraints.AddHorizontal(hex.Item(1))
		
'Dim oOffsetDim As OffsetDimConstraint
'		oOffsetDim = sketch1.DimensionConstraints.AddOffset()



'	ThisDoc.Save
End Sub



polygon add method.png

 

 

 

Please mark this response as Problem Solved if it answers your question.
----------------------------------------------------------------------------------------------
Ravi Kumar MB,
HP Z220 SFF Workstation
Autodesk Inventor Certified professional 2016
Email: ravikmb5@gmail.com





Message 5 of 8

austen_27_t
Contributor
Contributor

I ended up figuring it out 

0 Likes
Message 6 of 8

arron.craig
Collaborator
Collaborator

It would be appreciated if you could share the code, others may stumble on this looking for an answer

0 Likes
Message 7 of 8

austen_27_t
Contributor
Contributor

Dim sides As Long
sides = 6

'create new reference sketch plane
Dim sketch As PlanarSketch
Set sketch = pdef.Sketches.Add(pdef.WorkPlanes.Item(3))

'Sets reference to tg transient geometry
Dim tg As TransientGeometry
Set tg = ThisApplication.TransientGeometry

'Create lines for polygon
Call sketch.SketchLines.AddAsPolygon(sides, tg.CreatePoint2d(0, 0), tg.CreatePoint2d(0, dim1), False)

0 Likes
Message 8 of 8

ravikmb5
Collaborator
Collaborator

here is the Completed Code of ilogic

added dimensions

 

Sub Main()


	' Defining This Document
	Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
	'Definining a Component Definition
	Dim oPartDef As PartComponentDefinition = oPartDoc.ComponentDefinition
	Dim oParams As Parameters
	oParams = oPartDef.Parameters
	Dim oUserParams As UserParameters
	oUserParams = oParams.UserParameters
	    Dim oPolygonfaces As Long
  
    
'''Getting Data From User
   Dim oPolygonsides As Long
   oPolygonsides = InputBox("No of Polygon Sides", "Enter No of Sides of Polygon", "5")



	'[ Create's a Profile Sketch
	Dim sketch1 As PlanarSketch
	sketch1 = oPartDef.Sketches.Add(oPartDef.WorkPlanes.Item(3))
	sketch1.Name = "Profile Extrude Sketch"
	'Collecting Transient Geometry
	Dim tg As TransientGeometry
	tg = ThisApplication.TransientGeometry





	'''CentreOrigin Projected
	 Dim oProjectionPoint As SketchPoint
	oProjectionPoint = sketch1.AddByProjectingEntity(oPartDef.WorkPoints.Item(1))

	'''PolyGon Sketch
		Dim hex As SketchEntitiesEnumerator
		hex = sketch1.SketchLines.AddAsPolygon(oPolygonsides,oProjectionPoint,tg.CreatePoint2d(-0.5,-0.6882),True)
		
	''' Construcyion Circle	
		Dim oCir As SketchCircle
		oCir = sketch1.SketchCircles.AddByCenterRadius(oProjectionPoint, 1.3764 / 2)
		oCir.Construction = True
		
		
		'' Constrained Circle tangent to Lines of Polygon
		Call sketch1.GeometricConstraints.AddTangent(hex.Item(1), oCir)
		Call sketch1.GeometricConstraints.AddTangent(hex.Item(2),oCir)
		Call sketch1.GeometricConstraints.AddTangent(hex.Item(3),oCir)
		
		
		''' Circle Centre Point to Origin Point Merge
		Call oCir.CenterSketchPoint.Merge(oProjectionPoint)
		
		''' Adding Horizontal Constraint to one of the Lines of Polygon
		Call sketch1.GeometricConstraints.AddHorizontal(hex.Item(1))
		
		Dim oSketchLine As SketchLine
		oSketchLine = hex.Item(1)
		
Dim oOffsetDim As TwoPointDistanceDimConstraint
		oOffsetDim = sketch1.DimensionConstraints.AddTwoPointDistance(oSketchLine.EndSketchPoint, oSketchLine.StartSketchPoint,DimensionOrientationEnum.kHorizontalDim,tg.CreatePoint2d(0,-1.2), False)
oOffsetDim.Parameter.Expression = "Length = 10 mm"

Call sketch1.Solve

'	ThisDoc.Save
End Sub

poly 5 sides.pngPoly 6 Sides.png

Please mark this response as Problem Solved if it answers your question.
----------------------------------------------------------------------------------------------
Ravi Kumar MB,
HP Z220 SFF Workstation
Autodesk Inventor Certified professional 2016
Email: ravikmb5@gmail.com





0 Likes