Automatically Edit Object Properties in Plant 3D with VBA

Automatically Edit Object Properties in Plant 3D with VBA

joshuaadams
Advocate Advocate
5,008 Views
2 Replies
Message 1 of 3

Automatically Edit Object Properties in Plant 3D with VBA

joshuaadams
Advocate
Advocate

Hi All,

 

I've got a interesting question for you, that I havent been able to resolve myself, as follows:

 

Background: I have a suite of 50 Plant 3D models that have been created. However, none of the models have spool numbers assigned to them.

 

Question: Is it possible to edit the properties of pipe or any object in Plant 3D using VBA or VB.NET? (To avoid manually opening up each model and assigning spool numbers to each segment of pipe in the model).

 

Currrently I am able to do the following with Plant 3D myself:

 

1. Open Plant 3D

2. Open specified drawing

3. Select all objects in Plant 3D model

4. Output object names to the immediate window in VBA

5. Control certain aspects of the objects with the PLANT3D COM LIBRARY - This is where I am running into issues, the COM library doesn't appear to have a build in "spool property" to edit.


Please find sample code below for your reference.

 

Sub Plant_3D_Edit_Attributes()

Dim plant_app As AutoCAD.AcadApplication
Dim doc As AcadDocument
Dim appname As String
Dim sht_data As Worksheet
Dim objSS As AcadSelectionSet
Dim ent As AcadEntity
Dim pline As AcadLWPolyline
Dim FilterType(0) As Integer
Dim FilterData(0) As Variant
Dim Groupcode As Variant
Dim DataValue As Variant
Dim i As Integer
Dim pipe As PnP3dCOMLib.AcPnP3dPipeInlineAsset

'Creating AutoCAD Plant 3D instance
appname = "'acad.exe'"
Call Create_Model.App_Open(appname)

'This section of code is sometimes weird and wont create the plant 3D instance until it is opened once in windows.
If Create_Model.App_Open(appname) = "True" Then

'AutoCAD open so do nothing
Set plant_app = GetObject(, "AutoCAD.Application.19")

Else

Set plant_app = CreateObject("AutoCad.Application.19") 'ACAD Plant 3D needs to be set as the default ACAD application
plant_app.Visible = True

End If

Set doc = plant_app.Documents.Open("C:\Josh\Model Generation Environment\Plant 3D Projects\PLPR Test Project\Plant 3D Models\Plant 3D Model - K48.dwg")

'Create selection set for finding all paperspace blocks
On Error Resume Next

doc.SelectionSets.Item("ss").Delete

On Error GoTo 0

'Create selection set
Set objSS = doc.SelectionSets.Add("ss")

'Defining filter variables
FilterType(0) = 67 'Select all
FilterData(0) = 0 '0 = Model Space 1 = Paper Space
Groupcode = FilterType
DataValue = FilterData

'Select all objects in paper space
objSS.Select acSelectionSetAll, , , Groupcode, DataValue

For i = 1 To objSS.count - 1

Debug.Print objSS.Item(i).ObjectName 'Prints name component type, i.e. flange, gasket etc.

If objSS.Item(i).ObjectName = "AcPpDb3dPipeInlineAsset" Then

Set pipe = objSS.Item(i)


End If

Next i

End Sub

 

Thanks alot in advance for the assistance.

 

Cheers,

 

Josh

 

0 Likes
Accepted solutions (1)
5,009 Views
2 Replies
Replies (2)
Message 2 of 3

jabowabo
Mentor
Mentor
The spool property and most other 'piping' props are not found in the dwg database. They are located in the external SQL/SQLite db. You have to access them by finding the db table and row the prop resides in. There are some examples on the DevBlog:
http://adndevblog.typepad.com/autocad/plant3d/
Message 3 of 3

joshuaadams
Advocate
Advocate
Accepted solution

Hi JaboWabbo,

 

Thanks for the response, you've given me the hint I needed to resolve this issue! If you're interested the following is a summary of how you can automatically edit all object properties with VBA...

 

Given the project data for plant3D is managed in a SQLite (or SQL Server if you project is configured for this) database, you can actually use ADO with the appropriate ODBC drivers and connect to the database using VBA. Once you have established this connection, you can pass SQL statements to the database engine which then updates the required information. In my case I simply passed the following SQL Statement...

 

UPDATE `PipeRunComponent` SET `SpoolNumber`='RC-SPOOL-01'

 

Which updated all the piping compoents in my model with the "RC-SPOOL-01" spool number. Instructions to get this working on your system are provided below:

 

1. Download the SQLite3 ODBC Driver, link provided: http://www.ch-werner.de/sqliteodbc/, sqliteodbc.exe (make sure you download the 32bit vesion).

2. Create a new VBA module and add the "Microsoft ActiveX Data Objects 6.1 Library"

3. Use the following code to make your connection and execute your SQL.

 

Option Explicit
Sub P3D_DB_Connection()

'Define variables
Dim cn As ADODB.Connection
Dim RS As ADODB.Recordset
Dim i As Integer
Dim SQLString As String

'Create objects
Set cn = New ADODB.Connection
Set RS = New ADODB.Recordset

'Open connection to Plant3D project database
With cn

.ConnectionString = "DRIVER={SQLite3 ODBC Driver};Database=C:\josh\piping.dcf" '.DCF Files are the project database files they are usually located in your P3D project folder
.Open

End With

'Define SQL String to pass to database engine
SQLString = "SELECT * FROM PipeRunComponent"

'Execute the SQL code
With RS

.Source = SQLString
.ActiveConnection = cn
.CursorLocation = adUseClient
.CursorType = adOpenForwardOnly
.LockType = adLockReadOnly
.Open

End With

'Paste information to excel spreadsheet
Worksheets("Sheet5").Range("A1").CopyFromRecordset RS

'Close the connection
cn.Close

'Clear variables
Set cn = Nothing
Set RS = Nothing

End Sub

 

Cheers,

 

Josh

0 Likes