Deleting work planes based off their position/offset

Deleting work planes based off their position/offset

Patrick_JohnsonSU3NC
Explorer Explorer
348 Views
3 Replies
Message 1 of 4

Deleting work planes based off their position/offset

Patrick_JohnsonSU3NC
Explorer
Explorer

I have work planes in my template part that are used for constraining different components in an assembly.  There will be situations that the end use part will not need all of them and I would like to delete them from the end use part.  I have it set up so that if a plane will not be used, its offset from the origin will be set to a specific value.  How do I go about finding these planes and deleting them?

0 Likes
Accepted solutions (1)
349 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @Patrick_JohnsonSU3NC.  That seems like an odd request, so it is a little odd to do by code without more detailed information than that.  There are lots of ways do 'define' a WorkPlane, and which way you used will make a difference in the code used to find them, since we can't just find the ones you want by their name.  We must determine how each WorkPlane was defined, then figure out how to get the defining data from its definition, such as an offset value.  However, that will be pointing to a Inventor API Parameter object in this case.  The Parameter has a 3 main properties for figuring out what its value is, and two of them will always return the value in 'database units' (centimeters for distance/length), instead of the parameter's own units.  The other property (Expression) is like what you see in the 'equation' column of the Parameters dialog box, and is a String, including the units specifier text, instead of a true numerical value that you can do math with or compare with other numerical values.  So, when you specify what offset value you want to find, you must do so in centimeters (using math if needed), and/or specify the exact String, exactly as you see it in the Parameters dialog equation column.

 

Below is an example iLogic rule code you can play around with.  I put comment in there showing you where to set the values you are looking for.

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
	If oPDoc Is Nothing Then Return
	'<<< CHANGE VALUES OF THESE NEXT TWO VARIABLES >>>
	Dim dOffsetValueToFind As Double = (12.5 / 2.54) 'specify in centimeters
	Dim sOffsetExpressionToFind As String = "12.5 in"
	Dim oWPlanes As WorkPlanes = oPDoc.ComponentDefinition.WorkPlanes
	For Each oWPlane As WorkPlane In oWPlanes
		If oWPlane.IsCoordinateSystemElement Then Continue For
		'If Not oWPlane.Consumed Then oWPlane.Delete(False) 'False  = Do Not Retain Dependents
		If oWPlane.DefinitionType = WorkPlaneDefinitionEnum.kPlaneAndOffsetWorkPlane Then
			Dim oPOWPDef As PlaneAndOffsetWorkPlaneDef = oWPlane.Definition
			Dim oParam As Inventor.Parameter = oPOWPDef.Offset
			Dim dOffSetVal As Double = oParam.Value 'will be in 'database units' (centimeters)
			Dim sOffSetExpression As String = oParam.Expression
			'<<< HERE IS WHERE THE CHECK IS HAPPENING >>>
			If dOffSetVal = dOffsetValueToFind OrElse
				sOffSetExpression = sOffsetExpressionToFind Then
				Try
					oWPlane.Delete(False) 'False  = Do Not Retain Dependents
				Catch
					'what to do if that fail, like a message
				End Try
			End If
		End If
	Next
	oPDoc.Update2(True)
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4

Patrick_JohnsonSU3NC
Explorer
Explorer

Hey @WCrihfield , thank you so much for the detailed response.  I will mess around with your example code for what I am trying to accomplish.

 

I can provide more detailed information if that would help clear up my, probably poorly explained, post and maybe help someone else in the future.  Let me know. 

0 Likes
Message 4 of 4

nstevelmans
Advocate
Advocate
Accepted solution

Hi, maybe this will do the trick

 

   Dim offset As Double = 5  '50 mm offset
   Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
   Try
       ' Get the component definition of the part
       Dim compDef As PartComponentDefinition = oPartDoc.ComponentDefinition
       ' Get the workplanes collection
       Dim workPlanes As WorkPlanes = compDef.WorkPlanes
       ' Loop through the workplanes in reverse order (to avoid index shifting during deletion)
       Dim obc As ObjectCollection
       For i As Integer = workPlanes.Count To 1 Step -1
           Dim workPlane As WorkPlane = workPlanes.Item(i)
           Dim oPoint As Point = workPlane.Plane.RootPoint
           Dim z As Double = oPoint.Z
           If offset = oPoint.Z Then
               workPlane.Delete()
           End If
       Next
   Catch ex As Exception

   End Try

 

 

 

If a response answers your question, please use  ACCEPT SOLUTION  to assist other users later.

Also be generous with Likes!  Thank you and enjoy!