Friday, October 4, 2019

PROPERTIES TO PARAMETERS (P2P) and some VBA!

Did not quite make the weekly update as discussed in the last posting, but two weeks is still better than three years.

In the past we had pushed properties into a parameter so the parameter could be used in a sketch to create a feature, but due my faulty memory, I could not recall how it was accomplished previously. 

So some research was undertaken and some iLogic was created.

This post at cadline community was used to develop the iLogic code.

Sub Main()

 Dim oDoc As Inventor.PartDocument 

 If ThisDoc.Document.DocumentType <> kPartDocumentObject Then
  Exit Sub
 Else
  oDoc = ThisDoc.Document
 End If

 Dim customPropSet As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
 Dim CustomPropName As String = "Catalog Number"
 Dim CustomPropValue As String 
 Dim DefaultCustomPropValue As String = "NOT FOUND!"

 Dim oParams As Parameters
 oParams=oDoc.ComponentDefinition.Parameters

 Dim oUserParams As UserParameters
 oUserParams=oParams.UserParameters

 Dim UParamName As String = "CATALOG_NUMBER"

 ' Look for the desired custom user property
 Try 
  CustomPropValue = customPropSet.Item(CustomPropName).Value
 Catch
  CustomPropValue = DefaultCustomPropValue
 End Try

 ' Look for the desired user parameter
 Try
  oUserParams(UParamName).Value = CustomPropValue 
 Catch
  ' assume error means not found and create it
  oUserParams.AddByValue(UParamName , CustomPropValue, "text")
 End Try

End Sub

Code formatting courtesy of http://hilite.me/


HPS bronze stud connector showing the catalog number, which is a custom iProperty.

Below is a screen snippet after editing the catalog number.


Per the rules for keeping the model as simple as possible, most of the fillets have been suppressed in the model file, simply by moving the End of Part up the tree.


We also were looking for an approach to document named work features which need to match a standard name in order to provide automation for an Inventor add-in (www.substationdesignsuite.com).

We did some test code using VBA, and were able to tag (with a leader) the named user points in the drawing, but if the work point name was revised, then the leader would need to be created again.


Option Explicit

Sub Main()

Dim oLdrPoints As ObjectCollection
Set oLdrPoints = ThisApplication.TransientObjects.CreateObjectCollection

' Get view from user
Dim drawDoc As Document
Set drawDoc = ThisApplication.ActiveDocument

If Not (TypeOf drawDoc Is DrawingDocument) Then
    Exit Sub
End If

Dim oSheet As Sheet
Set oSheet = drawDoc.ActiveSheet

' Select a workpoint to be labeled
Dim oSelect As New clsSelect

MsgBox "Select workpoint to be labeled."

Dim oDwgCenterPoint As Object
Set oDwgCenterPoint = oSelect.Pick(kDrawingCentermarkFilter) ' (kDrawingCurveSegmentFilter)

Dim LeaderStartPoint As point2D
Set LeaderStartPoint = oDwgCenterPoint.Position

Dim endPoint As point2D
Dim GetPoint As New clsGetPoint
Set endPoint = GetPoint.GetDrawingPoint("Click the desired location for the tag.", kLeftMouseButton)

' Add points to the point collection
oLdrPoints.Add endPoint     ' Point for location of sketch symbol
oLdrPoints.Add LeaderStartPoint   ' Point for location of leader arrow

'' Sub PlaceLeader(oWP As Variant, oSheet As Sheet, oLeaderPoints As ObjectCollection)
Call PlaceLeader(oDwgCenterPoint, oSheet, oLdrPoints)

' To Do:
'   9/26/19
'   Add the ability to add in a colleciton
'   of points
'   At the end we need to...
'   Convert to dot.net

' Cleanup Time!
Cleanup:
    Set oSelect = Nothing
    Set oLdrPoints = Nothing
    Set drawDoc = Nothing
    Set oSheet = Nothing

End Sub



Sub PlaceLeader(oWP As Variant, oSheet As Sheet, oLeaderPoints As ObjectCollection)

''PURPOSE : PLACE A LEADER ON THE SHEET WITH THE NAME OF THE WORKFEATURE

Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry

Dim oPoint2D As point2D
Set oPoint2D = oWP.Position

Dim oWorkPoint As WorkPoint
Set oWorkPoint = oWP.AttachedEntity


' Create an intent and add to the ObjectCollection
Dim oGeoIntent As GeometryIntent
Set oGeoIntent = oSheet.CreateGeometryIntent(oWorkPoint)

Call oLeaderPoints.Add(oPoint2D)

Dim LdrText As String
LdrText = oWP.ModelWorkFeature.Name

Dim oLdrNote As LeaderNote
Set oLdrNote = oSheet.DrawingNotes.LeaderNotes.Add(oLeaderPoints, LdrText)

Dim oFirstNode As LeaderNode
Set oFirstNode = oLdrNote.Leader.RootNode.ChildNodes.Item(1)

Dim oSecondNode As LeaderNode
Set oSecondNode = oFirstNode.ChildNodes.Item(1)

Call oFirstNode.InsertNode(oSecondNode, oTG.CreatePoint2d(oLeaderPoints.Item(2).X, oLeaderPoints.Item(2).Y))

'Stop

End Sub

Part file with named point.


Drawing with leader text extracted from point name.

Next on the list is to convert the VBA code into VB.net code so we can complete the addin.  Most likely will utilize an attribute to link the drawing leader to the actual point so that the value can be updated if the model is revised.

Tune in next time for our notes on creating your own custom content center conduit fittings!









No comments:

Post a Comment