Error Deleting Workplanes

Error Deleting Workplanes

jianchen.yu
Explorer Explorer
284 Views
4 Replies
Message 1 of 5

Error Deleting Workplanes

jianchen.yu
Explorer
Explorer

I'm having some issues with the code below, it doesn't allow me to delete the workplanes before creating new ones, and it only fails in certain circumstances which doesn't seem relevant at all. The error is on the code "oWPlane.Delete". Please help.

 

Error on line 14 in rule: Work Plane Creation (indi.), in document: assembly.iam

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

Sub Main
	
	oDef = ThisDoc.Document.ComponentDefinition
	Dim oWPlane As WorkPlane
	oSurfaces = oDef.WorkPlanes
	Dim name As String
	
	For Each oWPlane In oSurfaces
		 name = oWPlane.Name
		If Left(name, 3) = "dim"
          	    oWPlane.Delete	
	  	End If	
	Next
	

	
	myDoubleValues = New Double() {x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7, x8, y8, x9, y9, x10, y10, x11, y11, x12, y12 }
	Dim arrayAnchorX(anchorBoltNo - 1) As Double
	Dim arrayAnchorY(anchorBoltNo - 1) As Double

	For i = 0 To anchorBoltNo - 1
		arrayAnchorX(i) = myDoubleValues(i * 2)
		arrayAnchorY(i) = myDoubleValues(i * 2 + 1)
	Next
	
	Dim arrayAnchorUniqueX(0) As Double 
	Dim arrayAnchorUniqueY(0) As Double
	arrayAnchorUniqueX(0) = arrayAnchorX(0)
	arrayAnchorUniqueY(0) = arrayAnchorY(0)
	
	'Delete all the duplicate dimension
	For i = 1 To anchorBoltNo - 1
		
		If Not arrayAnchorUniqueX.Contains(arrayAnchorX(i)) Then
			Array.Resize(arrayAnchorUniqueX, arrayAnchorUniqueX.Length + 1)
			arrayAnchorUniqueX(arrayAnchorUniqueX.Length - 1) = arrayAnchorX(i)
		End If
		
		If Not arrayAnchorUniqueY.Contains(arrayAnchorY(i)) Then
			Array.Resize(arrayAnchorUniqueY, arrayAnchorUniqueY.Length + 1)
			arrayAnchorUniqueY(arrayAnchorUniqueY.Length - 1) = arrayAnchorY(i)
		End If
		
	Next
	
	Array.Sort(arrayAnchorUniqueX)
	Array.Sort(arrayAnchorUniqueY)
	anchorDimQtyX = arrayAnchorUniqueX.Length
	anchorDimQtyY = arrayAnchorUniqueY.Length
	
	For i = 0 To arrayAnchorUniqueY.Length - 1
		
		oWPlane = AddWorkPlaneInAssemblyOffset(oDef.WorkPlanes("XY Plane"), -arrayAnchorUniqueY(i))
		oWPlane.Name = "dimPlaneXY" & i
		'oWPlane.Visible = False
	Next
	
	For i = 0 To arrayAnchorUniqueX.Length - 1
		
		oWPlane = AddWorkPlaneInAssemblyOffset(oDef.WorkPlanes("YZ Plane"), arrayAnchorUniqueX(i))
		oWPlane.Name = "dimPlaneYZ" & i
		'oWplane.Visible = False
	Next
	
End Sub

Function AddWorkPlaneInAssemblyOffset(oFace As Object, oOffset As Double) As WorkPlane
	Dim uOM As UnitsOfMeasure = ThisDoc.Document.UnitsOfMeasure
	Dim oAsm As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oDef As AssemblyComponentDefinition = oAsm.ComponentDefinition
	Dim oOriginPnt As Point
	Dim oXaxis As UnitVector
	Dim oYaxis As UnitVector
	oOriginPnt = ThisApplication.TransientGeometry.CreatePoint(0#, 0#, 0#)
	oXaxis = ThisApplication.TransientGeometry.CreateUnitVector(1#, 0#, 0#)
	oYaxis = ThisApplication.TransientGeometry.CreateUnitVector(0#, 1#, 0#)
	Dim oPlane As WorkPlane = oDef.WorkPlanes.AddFixed(oOriginPnt, oXaxis, oYaxis)
	oPlane.AutoResize = True
	oDef.Constraints.AddFlushConstraint(oFace, oPlane, uOM.ConvertUnits(oOffset, uOM.LengthUnits, UnitsTypeEnum.kDatabaseLengthUnits))
	oPlane.AutoResize = False
	Return oPlane
End Function

 

0 Likes
285 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @jianchen.yu.  Is it possible that any of those WorkPlanes that you are trying to delete may have dependents?  If the WorkPlane has a sketch on it, is being used by a feature, or any other WorkPlane is based off of the WorkPlane, it has dependents, and will not delete correctly, unless maybe you specify the optional input Boolean as True in that Delete method.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

jianchen.yu
Explorer
Explorer
Thanks @WCrihfield, I've checked and there're no dependents on those workplanes I want to delete and I also tried oWPlane.Delete(True) and it still didn't work. Any other things I can try or is there another approach to this?

Thank you!
0 Likes
Message 4 of 5

JMGunnar
Collaborator
Collaborator

@jianchen.yu 

 

ilogic code work find on my computer 

 

can you upload empty assemblies that's not working 

 

Johan

0 Likes
Message 5 of 5

jianchen.yu
Explorer
Explorer
@Anonymous, I fixed it. Turned out that if I delete all the constraints associated with those planes, the oWPlane.Delete will work.

Thanks,
JC
0 Likes