'Try
	Dim time As String = Now()
	time = time.Replace(":", "_")
	time = time.Replace(".", "_")
	time = time.Replace(" ", "_")
	
	
	If ThisApplication.ActiveDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

		'MsgBox(time)

'		Dim seznam As New List(Of String)

'		seznam.Add("Model")
'		seznam.Add("Polotovar")
'		seznam.Add("Upinky")

'		Dim volba = InputListBox("Co exportujete?", seznam, seznam.Item(1), Title := "Title", ListName := "List")

		Dim g_App As Inventor.InventorServer = ThisApplication
		Dim AssDoc As Inventor.AssemblyDocument= ThisDoc.Document

		Dim oAsmDoc As AssemblyDocument
		oAsmDoc = ThisApplication.ActiveDocument

		Dim oCompOcc As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Vyberte model k exportu")
		
		If oCompOcc Is Nothing Then
			Return
		End If
			
		Dim oPartDoc As PartDocument
		oPartDoc = g_App.Documents.Add(DocumentTypeEnum.kPartDocumentObject, , True)

		Dim oPartDef As PartComponentDefinition
		oPartDef = oPartDoc.ComponentDefinition

		Dim oDerivedAssemblyDef As DerivedAssemblyDefinition
		oDerivedAssemblyDef = oPartDef.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(AssDoc.FullDocumentName)
		' Set various shrinkwrap related options
		oDerivedAssemblyDef.DeriveStyle = DerivedComponentStyleEnum.kDeriveAsMultipleBodies
		oDerivedAssemblyDef.IncludeAllTopLevelWorkFeatures = DerivedComponentOptionEnum.kDerivedExcludeAll
		oDerivedAssemblyDef.IncludeAllTopLevelSketches = DerivedComponentOptionEnum.kDerivedExcludeAll
		oDerivedAssemblyDef.IncludeAllTopLeveliMateDefinitions = DerivedComponentOptionEnum.kDerivedExcludeAll
		oDerivedAssemblyDef.IncludeAllTopLevelParameters = DerivedComponentOptionEnum.kDerivedExcludeAll
		Call oDerivedAssemblyDef.SetHolePatchingOptions(DerivedHolePatchEnum.kDerivedPatchNone)
		Call oDerivedAssemblyDef.SetRemoveByVisibilityOptions(DerivedGeometryRemovalEnum.kDerivedRemoveNone)

		Dim oDerivedOccs As DerivedAssemblyOccurrences 
		oDerivedOccs = oDerivedAssemblyDef.Occurrences
		  
		For Each oOcc In oDerivedOccs
			If Not oOcc.Name = oCompOcc.Name Then
				oOcc.InclusionOption = DerivedComponentOptionEnum.kDerivedExcludeAll
			End If
		Next
			
		Dim oDerivedAss As DerivedAssemblyComponent
		oDerivedAss = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAssemblyDef)

		Call oDerivedAss.BreakLinkToFile()

		' Save the part

		Dim partname As String = ""
		partname = AssDoc.FullFileName & time & ".ipt"
		'MsgBox(partname)

		'Dim oCompOccName As String = oCompOcc.Name.Replace(":", "_") 'nahrazeni dvojtecky v nazvu souboru

		ThisApplication.ActiveView.Fit
		ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute

		If System.IO.File.Exists(partname) Then
			'Dim overWriteFile = MessageBox.Show("Pomocný soubor ipt s názvem " & partname & " již existuje, přepsat?", "Title", MessageBoxButtons.YesNo)
			'If overWriteFile = vbYes Then
				'MsgBox("Cesta souboru bude " & partname)
				Call oPartDoc.SaveAs(partname, False)
				
			'Else 
		'		Return
		'	End If
					
		Else
		    Call oPartDoc.SaveAs(partname, False)
		End If

		'MsgBox("Pomocny soubor ulozen " & System.IO.File.Exists(partname))

		Dim STLAddIn As TranslatorAddIn
		STLAddIn = ThisApplication.ApplicationAddIns.ItemById("{81CA7D27-2DBE-4058-8188-9136F85FC859}")
			
		Dim oContext As TranslationContext
		oContext = ThisApplication.TransientObjects.CreateTranslationContext
		oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
		Dim oOptions As NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap

		Dim oDataMedium As DataMedium
		oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

		If STLAddIn.HasSaveCopyAsOptions(oPartDoc.ComponentDefinition.Document, oContext, oOptions) Then
			oOptions.Value("OutputFileType") = 0 ' 0 = Binary , 1 = ASCII
			oOptions.Value("ExportUnits") = 5 ' units = mm
			oOptions.Value("Resolution") = 0  
			oOptions.Value("ExportColor") = True
		End If

		'Varianty pojmenovani souboru a jeho cesty

		'Varianta stejne slozky i jmena souboru, kde je sestava.
		'oDataMedium.FileName = ThisDoc.ChangeExtension(".stl")

		'Varianta stejneho jmena souboru komponenty ve slozce temp.
		Dim oCompOccName As String = oCompOcc.Name.Replace(":", "_") 'nahrazeni dvojtecky v nazvu souboru
		oDataMedium.FileName = "C:\Temp\" & oCompOccName & ".stl"
		'MessageBox.Show("Chci ulozit " & oDataMedium.FileName , "Info")

		Call STLAddIn.SaveCopyAs(oPartDoc.ComponentDefinition.Document, oContext, oOptions, oDataMedium)

		'MessageBox.Show("Saved " & oDataMedium.FileName , "Info")

		oPartDoc.Close(True)
		oAsmDoc.Activate

		System.IO.File.Delete(partname)

		Dim oProName As String = "CimcoStl" & volba
		'MsgBox(oProName)

		Dim oProSet As Inventor.PropertySet = ThisApplication.ActiveDocument.PropertySets.Item("User Defined Properties")
		' Look for custom iproperty and try to use it

		' oAsmDoc.PropertySets.Item("User Defined Properties")

		Dim oProp As Inventor.Property
		Try
		    oProp = oProSet.Item(oProName)
		Catch
		    ' iProperty not found , create and assign value
		    oProp = oProSet.Add("",oProName)
		End Try
		oProp.Value = oDataMedium.FileName

		partname = Nothing
	
	Else If ThisApplication.ActiveDocumentType = DocumentTypeEnum.kPartDocumentObject Then
'		Dim seznam As New List(Of String)
'		seznam.Add("Model")
'		seznam.Add("Polotovar Mesh")
'		seznam.Add("Polotovar Solid")
'		seznam.Add("Upinky")

'		Dim volba = InputListBox("Co exportujete?", seznam, seznam.Item(1), Title := "Title", ListName := "List")

		Dim g_App As Inventor.InventorServer = ThisApplication
		Dim partDoc As Inventor.PartDocument = ThisDoc.Document

		Dim oPartDoc As PartDocument
		oPartDoc = ThisApplication.ActiveDocument
	
		If volba <> "Polotovar Mesh" Then
			
			Dim oBody As SurfaceBody = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Vyberte těleso k exportu")
			
			If oBody Is Nothing Then
				Return
			End If
			
			Dim body As SurfaceBody
			
			For Each body In oPartDoc.ComponentDefinition.SurfaceBodies
				If Not body.Name = oBody.Name Then
					body.Visible = False
				End If
			Next
			
			For Each meshSet As MeshFeatureSet In oPartDoc.ComponentDefinition.MeshFeatureSets
				For Each mesh As MeshFeature In meshSet
						mesh.Visible = False
				Next
			Next
			
			Dim STLAddIn As TranslatorAddIn
			STLAddIn = ThisApplication.ApplicationAddIns.ItemById("{81CA7D27-2DBE-4058-8188-9136F85FC859}")
				
			Dim oContext As TranslationContext
			oContext = ThisApplication.TransientObjects.CreateTranslationContext
			oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
			Dim oOptions As NameValueMap
			oOptions = ThisApplication.TransientObjects.CreateNameValueMap

			Dim oDataMedium As DataMedium
			oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

			If STLAddIn.HasSaveCopyAsOptions(oPartDoc.ComponentDefinition.Document, oContext, oOptions) Then
				oOptions.Value("OutputFileType") = 0 ' 0 = Binary , 1 = ASCII
				oOptions.Value("ExportUnits") = 5 ' units = mm
				oOptions.Value("Resolution") = 0  
				oOptions.Value("ExportColor") = True
			End If

			If volba = "Polotovar Mesh" Then
				volba = "Polotovar"
			End If
			
			If volba = "Polotovar Solid" Then
				volba = "Polotovar"
			End If

			Dim oStlFileName As String = ThisDoc.FileName(False)
			oDataMedium.FileName = "C:\Temp\" & oStlFileName & "_" & volba & "_" & time & ".stl"

			Call STLAddIn.SaveCopyAs(oPartDoc.ComponentDefinition.Document, oContext, oOptions, oDataMedium)

			MessageBox.Show("Saved " & oDataMedium.FileName , "Info")
			
			Dim oProName As String = "CimcoStl" & volba
			
			Dim oProSet As Inventor.PropertySet = ThisApplication.ActiveDocument.PropertySets.Item("User Defined Properties")
			
			Dim oProp As Inventor.Property
			Try
			    oProp = oProSet.Item(oProName)
			Catch
			    oProp = oProSet.Add("",oProName)
			End Try
			oProp.Value = oDataMedium.FileName

			For Each body In oPartDoc.ComponentDefinition.SurfaceBodies
				body.Visible = True
			Next	
			
			For Each meshSet As MeshFeatureSet In oPartDoc.ComponentDefinition.MeshFeatureSets
				For Each mesh As MeshFeature In meshSet
					mesh.Visible = True
				Next
			Next
			
		Else If volba = "Polotovar Mesh" Then
				
			Dim oMesh As MeshFeature  = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartMeshFeatureFilter, "Vyberte mesh sit k exportu")
			
			If oMesh Is Nothing Then
				Return
			End If
			
			Dim body As SurfaceBody
			For Each body In oPartDoc.ComponentDefinition.SurfaceBodies
				body.Visible = False
			Next
			
			Dim meshSet As MeshFeatureSet
			For Each meshSet In oPartDoc.ComponentDefinition.MeshFeatureSets
				For Each mesh As MeshFeature In meshSet
					If Not mesh.Name = oMesh.Name Then
						mesh.Visible = False
					End If
				Next
			Next
			
			Dim STLAddIn As TranslatorAddIn
			STLAddIn = ThisApplication.ApplicationAddIns.ItemById("{81CA7D27-2DBE-4058-8188-9136F85FC859}")
				
			Dim oContext As TranslationContext
			oContext = ThisApplication.TransientObjects.CreateTranslationContext
			oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
			Dim oOptions As NameValueMap
			oOptions = ThisApplication.TransientObjects.CreateNameValueMap

			Dim oDataMedium As DataMedium
			oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

			If STLAddIn.HasSaveCopyAsOptions(oPartDoc.ComponentDefinition.Document, oContext, oOptions) Then
				oOptions.Value("OutputFileType") = 0 ' 0 = Binary , 1 = ASCII
				oOptions.Value("ExportUnits") = 5 ' units = mm
				oOptions.Value("Resolution") = 0  
				oOptions.Value("ExportColor") = True
			End If

			If volba = "Polotovar Mesh" Then
				volba = "Polotovar"
			End If
			
			If volba = "Polotovar Solid" Then
				volba = "Polotovar"
			End If

			Dim oStlFileName As String = ThisDoc.FileName(False)
			
			oDataMedium.FileName = "C:\Temp\" & oStlFileName & "_" & volba & "_" & time & ".stl"

			Call STLAddIn.SaveCopyAs(oPartDoc.ComponentDefinition.Document, oContext, oOptions, oDataMedium)

			MessageBox.Show("Saved " & oDataMedium.FileName , "Info")
			
			Dim oProName As String = "CimcoStl" & volba
			
			Dim oProSet As Inventor.PropertySet = ThisApplication.ActiveDocument.PropertySets.Item("User Defined Properties")
			
			Dim oProp As Inventor.Property
			Try
			    oProp = oProSet.Item(oProName)
			Catch
			    oProp = oProSet.Add("",oProName)
			End Try
			oProp.Value = oDataMedium.FileName


			For Each body In oPartDoc.ComponentDefinition.SurfaceBodies
				body.Visible = True
			Next	
			
			For Each meshSet In oPartDoc.ComponentDefinition.MeshFeatureSets
				For Each mesh As MeshFeature In meshSet
					mesh.Visible = True
				Next
			Next
			
			
		End If	
		
	End If
		
'Catch ex As Exception
'	ErrorMessage = "Error export stl file."
'End Try

'Logger.Info("hodnota parametru d72 je {0}",d72)
