Skip to content

Instantly share code, notes, and snippets.

@autokludge
Created September 17, 2021 12:58
Show Gist options
  • Save autokludge/7519786af9d51ab314640436bd7aa96f to your computer and use it in GitHub Desktop.
Save autokludge/7519786af9d51ab314640436bd7aa96f to your computer and use it in GitHub Desktop.
iLogic create segments example
Imports System
Sub main()
' @autokludge 2021
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim assembly_doc As AssemblyDocument = ThisDoc.Document
Dim assembly_component_def = assembly_doc.ComponentDefinition
Dim template_doc_name = "test-guggis-master.ipt"
Dim template_doc_path = filepath(template_doc_name)
Dim template_part_doc As PartDocument
Dim template_length_param = "SECTION_LENGTH"
Try
Logger.Info("Is this already open? {0}", {template_doc_path})
template_part_doc = ThisApplication.Documents.ItemByName(template_doc_path)
Logger.Info("Yes. grabbed it.")
Catch
Logger.Info("Nope. try opening it then.")
template_part_doc = ThisApplication.Documents.Open(template_doc_path, False)
End Try
'Get list of lenghts from parameter input string
Dim param_lengths As String = assembly_component_def.Parameters.UserParameters("LENGTHS").Value.ToString
Dim oa_length = assembly_component_def.Parameters.UserParameters("OA_LENGTH").Value ' probably in cm
Dim lengths = New ArrayList(param_lengths.Split(","))
'This makes sure that all values parse correctly, if not then cancel this
Logger.Info("===============")
Logger.Info("lengths")
Logger.Info("=======")
For Each l In lengths
Dim dbl
Try
dbl = Double.Parse(l)
Catch
MsgBox("Please ensure LENGTHS parameter is in form of num,num,num,num. no other characters.")
Return
End Try
Logger.Info(dbl.ToString)
Next
Logger.Info("=======")
'Transaction for undo
Dim tr As Transaction = ThisApplication.TransactionManager.StartTransaction(ThisDoc.Document, "Create Segments iLogic")
Try
Dim pos As Double = 0
For Each l In lengths
Dim dbl = Double.Parse(l)
Dim segment_name = "segment-%.ipt".Replace("%", dbl.ToString())
Dim segment_doc_path = filepath(segment_name)
Dim segment_position_matrix = oTG.CreateMatrix()
segment_position_matrix.SetTranslation(oTG.CreateVector(0, 0, pos/10)) 'pos in mm, document in cm
pos += dbl
' create segment file if it doesn't exist
If Not System.IO.File.Exists(segment_doc_path) Then
template_part_doc.SaveAs(segment_doc_path, True)
End If
' place component into assembly
Dim new_segment_occ = assembly_component_def.Occurrences.Add(segment_doc_path, segment_position_matrix)
Dim new_segment_def As PartComponentDefinition = new_segment_occ.Definition
new_segment_def.Parameters.UserParameters(template_length_param).Value = dbl/10 'dbl in mm, document in cm
new_segment_occ.Grounded = True
Next
Catch
tr.Abort()
End Try
tr.End()
End Sub
Function filepath(doc_filename As String) As String
Dim workspace_path = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
Return System.IO.Path.Combine(workspace_path, doc_filename)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment