Delete projected entity

Delete projected entity

tim11_manhhieu
Advocate Advocate
480 Views
12 Replies
Message 1 of 13

Delete projected entity

tim11_manhhieu
Advocate
Advocate

Hi experts,

I am writing a macro to use in assembly environment, to punch a threaded hole by picking edge of drill hole.

 

tim11_manhhieu_0-1747970520414.png

 

The problem is that after creating the threaded hole, there are projected entities left.

I want to delete the yellow sketch circle and yellow sketch point as shown in the image.

iLogic or VBA are both fine.

 

tim11_manhhieu_1-1747970535141.png

 

Option Explicit

Sub XXX()

Dim oAssemDoc As AssemblyDocument
Dim oAssemDef As AssemblyComponentDefinition
Dim oFace As Object
Dim oOcc As ComponentOccurrence
Dim oPartDoc As PartDocument
Dim oPartDef As PartComponentDefinition
Dim oSketch As PlanarSketch
Dim oEntity As Object
Dim oEntityProxy As Object
Dim oSketchProxy As Object
Dim oEntityOcc As ComponentOccurrence
Dim x As Double
Dim y As Double
Dim oHoleCenters As ObjectCollection
Dim oHoleTapInfo As HoleTapInfo
Dim oTransGeom As TransientGeometry
Dim h As HoleFeature

Set oAssemDoc = ThisApplication.ActiveDocument
Set oAssemDef = oAssemDoc.ComponentDefinition

Set oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Select face on part for new sketch") 
If oFace Is Nothing Then Exit Sub

Set oOcc = oFace.ContainingOccurrence
oOcc.Edit

Set oPartDoc = ThisApplication.ActiveEditDocument
Set oPartDef = oPartDoc.ComponentDefinition

Set oSketch = oPartDef.Sketches.Add(oFace.NativeObject)
oSketch.Edit

Set oEntity = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllEntitiesFilter, "Select entity to project")
If oEntity Is Nothing Then Exit Sub

Set oEntityOcc = oEntity.ContainingOccurrence
Call oEntityOcc.CreateGeometryProxy(oEntity, oEntityProxy)

Call oOcc.CreateGeometryProxy(oSketch, oSketchProxy)
Call oSketchProxy.AddByProjectingEntity(oEntityProxy)

oSketch.Adaptive = False

Set oTransGeom = ThisApplication.TransientGeometry

x = oSketch.SketchCircles.item(1).CenterSketchPoint.Geometry.x
y = oSketch.SketchCircles.item(1).CenterSketchPoint.Geometry.y

Set oHoleCenters = ThisApplication.TransientObjects.CreateObjectCollection
Call oHoleCenters.Add(oSketch.SketchPoints.Add(oTransGeom.CreatePoint2d(x, y)))

Set oHoleTapInfo = oPartDef.Features.HoleFeatures.CreateTapInfo(True, "ISO Metric profile", "M6x1", "6H", True)

Set h = oPartDef.Features.HoleFeatures.AddDrilledByThroughAllExtent(oHoleCenters, oHoleTapInfo, kPositiveExtentDirection)

oSketch.ExitEdit
ThisApplication.CommandManager.ControlDefinitions.item("AppReturnTopCmd").Execute

oOcc.Adaptive = False

End Sub

 

0 Likes
Accepted solutions (1)
481 Views
12 Replies
Replies (12)
Message 2 of 13

bradeneuropeArthur
Mentor
Mentor

How to define and keep that definition for the hole feature than?

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 13

WCrihfield
Mentor
Mentor

What about turning the visibility of that sketch off instead.  The sketch needs to be there, because the HoleFeature is dependent on it.

PlanarSketch.Visible = False ('oSketch.Visible = False' in your code example)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 13

WCrihfield
Mentor
Mentor

Another idea would be to use a WorkPoint as the location of the HoleFeature, instead of a projected sketch.  If doing it that way, you could use the HoleFeatures.CreatePointPlacementDefinition method to create a PointHolePlacementDefinition (uses a WorkPoint to define hole location).  We would usually have used the HoleFeatures.CreateSketchPlacementDefinition to create a SketchHolePlacementDefinition when defining the locations of HoleFeatures by way of SketchPoints, but they continue to support the older way of using an ObjectCollection containing SketchPoints also.  I would likely be a good idea to use the newer methods in any important future automation solutions though.  Just some additional thoughts.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 13

patrick.schlambHPACQ
Participant
Participant
Accepted solution

Alternatively, you can remove reference of the projected item and it will no longer be a projection. The following code will break the reference to the projection and then ground the entity so you aren't left with unconstrained sketches. 

For Each sketchEnt As SketchEntity In oSketch.SketchEntities
	sketchEnt.Reference = False
	Try
		oSketch.GeometricConstraints.AddGround(sketchEnt)
	Catch : End Try
Next sketchEnt  

 

Message 6 of 13

marcin_otręba
Advisor
Advisor

try:

 

 

Dim oProjEnt As SketchEntity = oSketchProxy.AddByProjectingEntity(oEntityProxy)

 

instead of 

Call oSketchProxy.AddByProjectingEntity(oEntityProxy)

and then just delete it after creating hole centers:

 

oProjEnt.Delete()

 

Hi, maybe you want to vote my:

Ideas

or check my apps:

DrawingTools   View&ColoringTools   MRUFolders

Message 7 of 13

tim11_manhhieu
Advocate
Advocate

Thank you for response.

They are not dependent on each other, after creating the hole, the hole will depend on the sketch point.

The projected entity can be deleted if we delete all their constraints.

 

0 Likes
Message 8 of 13

tim11_manhhieu
Advocate
Advocate

I'll look into HoleFeatures.CreatePointPlacementDefinition more.

Since I'm almost there, there's only one small issue left.

0 Likes
Message 9 of 13

tim11_manhhieu
Advocate
Advocate

I tried it and it worked, thank you.

 

sketchEnt.Reference = False

 

0 Likes
Message 10 of 13

tim11_manhhieu
Advocate
Advocate

Thank you for response. It still error.

0 Likes
Message 11 of 13

tim11_manhhieu
Advocate
Advocate

VBA for who need.

 

 

Option Explicit

Sub zzz()

Dim oAssemDoc As AssemblyDocument
Dim oAssemDef As AssemblyComponentDefinition
Dim oFace As Object
Dim oOcc As ComponentOccurrence
Dim oPartDoc As PartDocument
Dim oPartDef As PartComponentDefinition
Dim oSketch As PlanarSketch
Dim oEntity As Object
Dim oEntityProxy As Object
Dim oSketchProxy As Object
Dim oEntityOcc As ComponentOccurrence
Dim x As Double
Dim y As Double
Dim oHoleCenters As ObjectCollection
Dim oHoleTapInfo As HoleTapInfo
Dim oTransGeom As TransientGeometry
Dim h As HoleFeature
Dim oSketchEntity As SketchEntity

Set oAssemDoc = ThisApplication.ActiveDocument
Set oAssemDef = oAssemDoc.ComponentDefinition

Set oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Select plane for tap hole: ") 
If oFace Is Nothing Then Exit Sub

Set oOcc = oFace.ContainingOccurrence
oOcc.Edit

Set oPartDoc = ThisApplication.ActiveEditDocument
Set oPartDef = oPartDoc.ComponentDefinition

Set oSketch = oPartDef.Sketches.Add(oFace.NativeObject)
oSketch.Edit

Set oEntity = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceCylindricalFilter, "Select drill hole face: ")
If oEntity Is Nothing Then Exit Sub

Set oEntityOcc = oEntity.ContainingOccurrence
Call oEntityOcc.CreateGeometryProxy(oEntity, oEntityProxy)

Call oOcc.CreateGeometryProxy(oSketch, oSketchProxy)
Call oSketchProxy.AddByProjectingEntity(oEntity.Edges.item(1))

oSketch.Adaptive = False

Set oTransGeom = ThisApplication.TransientGeometry

x = oSketch.SketchCircles.item(1).CenterSketchPoint.Geometry.x
y = oSketch.SketchCircles.item(1).CenterSketchPoint.Geometry.y

Set oHoleCenters = ThisApplication.TransientObjects.CreateObjectCollection
Call oHoleCenters.Add(oSketch.SketchPoints.Add(oTransGeom.CreatePoint2d(x, y)))

Set oHoleTapInfo = oPartDef.Features.HoleFeatures.CreateTapInfo(True, "ISO Metric profile", "M6x1", "6H", True)

Set h = oPartDef.Features.HoleFeatures.AddDrilledByThroughAllExtent(oHoleCenters, oHoleTapInfo, kPositiveExtentDirection)

For Each oSketchEntity In oSketch.SketchEntities
    oSketchEntity.Reference = False
Next

oSketch.SketchCircles.item(1).Delete

oOcc.ExitEdit (kExitToTop)

oOcc.Adaptive = False

End Sub
0 Likes
Message 12 of 13

WCrihfield
Mentor
Mentor

I didn't actually say that the hole feature would be dependent on the projected geometry, but rather to the sketch, and did not think it would be wise to delete the sketch.  But now I understand you were just talking about breaking the 'projected Link', then deleting just that projected circle, instead of the sketch.  I also apparently did not pay close enough attention to one line in your code.  Since you are creating a 'new' SketchPoint within the sketch at the same 'location' of the projected circle's center, instead of finding then using the projected center point of the circle itself, that eliminated any dependency on the projected geometry.  If you had used the existing projected SketchPoint object to specify the hole feature's location, then deleting the projected sketch geometry would have likely broken the hole feature.  Also note that the new SketchPoint that you created is not 'grounded' or constrained in any way yet, so you may want to add that, if it is important.

Glad to see that you got it working OK.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 13 of 13

tim11_manhhieu
Advocate
Advocate

I have noted that SketchPoint is not dimensionally constrained.

I will manually constrain and that is also my intention.

During the design process, the hole position often has to be changed, so constraining the hole position is something that cannot be ignored.

The operation of punching the threaded hole below often takes a lot of time, so I found a solution for it.

0 Likes