Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
JelteDeJong
in reply to: JelteDeJong

Hi, i found the problem that i had last knight. the command form was closed but the command it self was not finished. i had to force it to stop. That did the trick. Below you find the working code. I also removed some code that could that loops over all nodes tofind the correct Browserpane and nodes. (and i used my own code from you previous question;-)

Public Class ThisRule

	Private busy As Boolean = False

	Sub Main()

		' Get the collection of control definitions.
		Dim oControlDefs As ControlDefinitions = ThisApplication.CommandManager.ControlDefinitions
		Dim oControlDef As ControlDefinition = oControlDefs.Item("AEC_Exchange:Command:Connector:Edit")

		' check if this document is a part document
		If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
			MessageBox.Show("The current document must be a part file", "Incorrect Document Type")
			Return
		End If

		'Enter the BIM Content environment
		ThisApplication.CommandManager.ControlDefinitions.Item("AEC_Exchange:Environment").Execute()

		'Access the Browser and Activate the "Bim Content" Browser Pane
		Dim oPartDoc As PartDocument = ThisDoc.Document
		Dim bps = oPartDoc.BrowserPanes
		Dim bimContentPane As BrowserPane = oPartDoc.BrowserPanes.Item("AEC_Exchange:Browser")

		'Define the Appearance variables
		Dim oElecAppearance As Asset = oPartDoc.Assets.Item("Connection_Electrical")
		Dim oMechAppearance As Asset = oPartDoc.Assets.Item("Connection_Mechanical")
		Dim oPipeAppearance As Asset = oPartDoc.Assets.Item("Connection_Piping")

		'Define the BIM Component Information
		Dim oPartDocBIM As BIMComponent = oPartDoc.ComponentDefinition.BIMComponent


		'Check through each Face on the body and see if uses the special appearance.
		For Each oSrfBody As SurfaceBody In oPartDoc.ComponentDefinition.SurfaceBodies
			For Each oFace As Face In oSrfBody.Faces
				If oFace.Appearance.DisplayName = oElecAppearance.DisplayName Then

					Dim Elec_Coll As ObjectCollection
					Elec_Coll = ThisApplication.TransientObjects.CreateObjectCollection
					Elec_Coll.Add(oFace)

					'Create Electrical Connector Definition
					Dim oElecConn As BIMElectricalConnectorDefinition

					oElecConn = oPartDocBIM.Connectors.CreateElectricalConnectorDefinition(Elec_Coll)
					oElecConn.SystemType = BIMElectricalSystemTypeEnum.kPowerBalancedElectricalSystemType

					'Add the Electrical BIM Connection
					Dim con As BIMConnector = oPartDocBIM.Connectors.Add(oElecConn)
					con.Name = "Elec_Conn_" & oPartDocBIM.Connectors.Count



					Dim systemNode As BrowserNode = bimContentPane.TopNode.BrowserNodes.Item("MEP System Connections")
					Dim electicalNode As BrowserNode = systemNode.BrowserNodes.Item("Electrical")
					electicalNode.Expanded = True
					Dim oConnNode As BrowserNode = electicalNode.BrowserNodes.Item(con.Name)
					oConnNode.DoSelect()

					oControlDef.Execute()
					Dim docEvents As DocumentEvents = ThisApplication.ActiveDocument.DocumentEvents
					AddHandler docEvents.OnChange, AddressOf docEvents_OnChange
					busy = True
					While (busy = True)
						System.Threading.Thread.Sleep(50)
						ThisApplication.UserInterfaceManager.DoEvents()
					End While
					RemoveHandler docEvents.OnChange, AddressOf docEvents_OnChange
					ThisApplication.CommandManager.StopActiveCommand()

				End If
			Next
		Next
	End Sub


	Private Sub docEvents_OnChange(ReasonsForChange As CommandTypesEnum, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)

		If (BeforeOrAfter = EventTimingEnum.kAfter And Context(1) = "API Change") Then
			busy = False
		End If

	End Sub

End Class

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com