Ilogic script to slice body and export slices to dxf?

Ilogic script to slice body and export slices to dxf?

bruce.blundell
Participant Participant
1,302 Views
11 Replies
Message 1 of 12

Ilogic script to slice body and export slices to dxf?

bruce.blundell
Participant
Participant

Hi All,

Do any of you know if there is a script that might allow you to specify a series of planes or surfaces

at that intersect a body at set heights, these then split the body, and export the slices as DXFs?

 

or anything that might do a similar job

 

Regards

Bruce 

0 Likes
1,303 Views
11 Replies
Replies (11)
Message 2 of 12

AlexFielder
Advisor
Advisor

Hi @bruce.blundell,

 

Ask and ye shall receive:

 

Option Explicit On
Imports Inventor.SelectionFilterEnum
''' <summary>
''' This rule asks the user to select a workplane and a point to decide on the number of slices.
''' The program then works out the spacing between the two selections.
''' It does require the workplane normal to be facing into the part as there is no intelligence to determine if that is the case after selection.
''' </summary>

Sub Main
	Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Slice and export tool")
	transGeom = ThisApplication.TransientGeometry
	Try
		Dim Cont As Boolean = True
		Dim Counter As Integer = 1

		Dim oDoc As Document = ThisDoc.Document
		If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject OrElse oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
			MessageBox.Show("This tool can only be used in a part file!", "No slicing for you!", _
			MessageBoxButtons.OK,MessageBoxIcon.Exclamation, _
			MessageBoxDefaultButton.Button1)
			Return
		End If

		Dim oPartCompDef As PartComponentDefinition = oDoc.ComponentDefinition
		Dim oWorkPlanes As WorkPlanes = oPartCompDef.WorkPlanes
		Dim oFilter As SelectionFilterEnum = SelectionFilterEnum.kWorkPlaneFilter

		' Turn origin planes on
		For a = 1 To 3
			oWorkPlanes.Item(a).Visible = True
		Next

		' Get start plane from user
		Dim oStartPlane As WorkPlane = ThisApplication.CommandManager.Pick(oFilter, "Select Start Plane")
		Dim slicingEndPoint As Object = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllPointEntities,"Select a point to slice between")
		Dim GridlineName As String = InputBox("Enter Name of Grid Planes", "Say my name!", "GL")

		If GridlineName = "" Then
			Cleanup(oWorkPlanes)
			Return
		End If
		
		Dim StartNo As Double = 10
	'	Dim Offset As String = InputBox("Enter Plane Offset in mm (From Previous)" & vbLf & vbLf & "Hit cancel to finish", Title, StartNo)
		Dim totaloffset As Double = ThisApplication.MeasureTools.GetMinimumDistance(oStartPlane, slicingEndPoint)
		Dim numPlanes As Double = CDbl(InputBox("Enter Number of planes" & vbLf & vbLf & "Hit cancel to finish", "Planes, Planes, Planes", StartNo))
		Dim offset As Double = totaloffset / numPlanes
		Dim oNewWorkPlane As WorkPlane
		'1 less than total slices because otherwise there's nothing to slice.
		For i As Integer = 0 To numPlanes -1
			Dim CountString As String = i.ToString("D4")
			If i = 0 Then
				oNewWorkPlane = oStartPlane
			Else If i = 1 Then
				oNewWorkPlane = oWorkPlanes.AddByPlaneAndOffset(oStartPlane, offset)
			Else
				oNewWorkPlane = oWorkPlanes.AddByPlaneAndOffset(oNewWorkPlane, offset)
			End If
			Try
				oNewWorkPlane.Name = "Work Plane" & GridlineName & CountString
			Catch
			End Try
			Dim newSketch As PlanarSketch = CreateSketch(oPartCompDef, oNewWorkPlane)
			oNewWorkPlane.Visible = False
			newSketch.ProjectedCuts.Add()
			newSketch.Edit()
			newSketch.ExitEdit()
			newSketch.Name = GridlineName & CountString
			ExportSketch(newSketch)
			newSketch.Visible = False
			If newSketch.SketchLines.Count > 0 Then
'				newSketch.exp
			End If
	'		offset += offset
		Next i
		Cleanup(oWorkPlanes)
		trans.End()
	Catch
		trans.Abort()
	End Try
End Sub

Sub ExportSketch(ByVal thisSketch As PlanarSketch)
	'Export face to pre-defined folder
	Dim selSet As SelectSet = ThisApplication.ActiveDocument.SelectSet
	If selSet.Count = 0 Then
		selSet.Select(thisSketch)
	End If
	Dim oFolder As String = ThisDoc.Path & "\" & ThisDoc.FileName & "DXF\"
	If Not System.IO.File.Exists(oFolder) Then System.IO.Directory.CreateDirectory(oFolder)
	Dim oFileName As String = oFolder & thisSketch.name & ".dxf"

	Dim oCmdMgr As CommandManager
	oCmdMgr = ThisApplication.CommandManager

	Call oCmdMgr.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent, oFileName)
	Call oCmdMgr.ControlDefinitions.Item("GeomToDXFCommand").Execute
End Sub

Public OriginSketchPoint As SketchPoint = Nothing
Public transGeom As TransientGeometry = Nothing

Sub Cleanup(ByVal oWorkPlanes As WorkPlanes)
' Turn origin planes off
For a = 1 To 3
    oWorkPlanes.Item(a).Visible = False
Next

End Sub

''' <summary>
''' Creates a sketch based on a workplane
''' </summary>
''' <param name="compDef"></param>
''' <param name="plane"></param>
''' <returns></returns>
Public Function CreateSketch(ByVal compDef As ComponentDefinition, ByVal plane As WorkPlane) As PlanarSketch
	Dim newSketch As PlanarSketch = compDef.Sketches.Add(plane)
	Call CorrectForMissingOriginPoint(compDef, newSketch)
	If Not newSketch Is Nothing Then
		Return newSketch
	Else
		Return Nothing
	End If
End Function

''' <summary>
''' Checks if the supplied planarsketch has the part originpoint projected into it (if created by the API it won't!) and if not adds it.
''' </summary>
''' <param name="compDef"></param>
''' <param name="thisSketch"></param>
Public Sub CorrectForMissingOriginPoint(ByVal compDef As ComponentDefinition, ByRef thisSketch As PlanarSketch)
	Dim oOriginWP As WorkPoint  = compDef.WorkPoints.Item(1)
    'set as global above
'    Dim oOriginSketchPoint As SketchPoint
    For Each oSketchPoint As SketchPoint In thisSketch.SketchPoints
        If oSketchPoint.ReferencedEntity Is oOriginWP Then
            OriginSketchPoint = oSketchPoint
            Exit For
        End If
    Next
   
    ' Create the sketch point for the origin, if it doesn't already exist.
    If OriginSketchPoint Is Nothing Then
        ' Project the origin point onto the sketch.
        OriginSketchPoint = thisSketch.AddByProjectingEntity(compDef.WorkPoints.Item(1))
    End If
End Sub

Here is the rule in action:

slice part and export to dxf.gif

 And attached is the sample part I built in Inventor 2019 along with the .dxf sample files created above.

 

Cheers,

 

Alex.

 

PS. This rule doesn't "split" the solid body as per your original request, but it could I believe be modified to do so fairly easily.

Message 3 of 12

bruce.blundell
Participant
Participant

Hi Alex,

 

Wow that's great, thanks for that

 

A few questions though.

I have found that the part needs to be saved as to get the workplanes and sketches to show up?

Is there a way that these sketches can then be automatically saved out?

 

Regards

Bruce

0 Likes
Message 4 of 12

bruce.blundell
Participant
Participant

Scratch that last one, the saving issue appears to be an issue with my machine only

works perfectly on a different machine

 

Regards

Bruce

0 Likes
Message 5 of 12

Anonymous
Not applicable

What would be a simple, code to use all surfaces to split a loft where they intersect?

I don't know how to change your code to do that. Is there any way you can help? 

0 Likes
Message 6 of 12

AlexFielder
Advisor
Advisor

Hi @Anonymous,

 

Do you have a sample file you can share to illustrate the problem?

 

Cheers,

 

Alex.

Message 7 of 12

Anonymous
Not applicable

Here is where i manually slice the loft and it is really time consuming. 

 

edgedesigner_0-1630504480317.png

 

0 Likes
Message 8 of 12

AlexFielder
Advisor
Advisor

Yikes! So you want a profile "slice" on each intersection with a workplane is that correct..?

Message 9 of 12

Anonymous
Not applicable

@AlexFielder wrote:

Yikes! So you want a profile "slice" on each intersection with a workplane is that correct..?


YES SIR, when i change the degrees in the form it also changes the slices, but now i have to re-slice everything again manually. Would be nice if the code could be generalized to slice everything always. (maybe even delete current slices and reslice). 

Is this possible? 

0 Likes
Message 10 of 12

AlexFielder
Advisor
Advisor

Okay, so this is going to pseudo-code but in theory if you can use a SelectionFilterEnum to create a collection of workplanes by simply dragging a selection box around them (I can't think why you wouldn't be able to do that given you can manually do it):

 

'Create your "collection" of workplanes

dim someCount as Int = 0

For each wp as Workplane in WorkplanesCollection
    Dim newSketch As PlanarSketch = CreateSketch(oPartCompDef, wp)
    oNewWorkPlane.Visible = False
    newSketch.ProjectedCuts.Add()
    newSketch.Edit()
    newSketch.ExitEdit()
    newSketch.Name = wp.name + someCount.Tostring
    ExportSketch(newSketch)
    someCount = someCount + 1
next

'Profit...? 🙂

 

Or something like that.

Message 11 of 12

Anonymous
Not applicable

here is the exact feature we are trying to split. 

BTW if we can figure out how to do this we will be able to change autodesk history. 

Here is why: everyone says you can only roll helical spirals. We figured out a way that we can use a single break press. Once i complete this template and automate it i will share with everyone here. 

This might even get autodesk developers to add this feature in the future. 

0 Likes
Message 12 of 12

mindink
Contributor
Contributor

hey this looks interesting, were you able to complete it?

0 Likes