select a specific sketch block instance via API

Programming and macros
User avatar
mp3-250
Posts: 630
Joined: Tue Sep 28, 2021 4:09 am
Answers: 20
Location: Japan
x 702
x 347

select a specific sketch block instance via API

Unread post by mp3-250 »

I have a 3D Sketch in an assembly and I insert some sketch block inside it.
In 3d sketch Edit mode, is there a way to retrieve the block instance name like "BLOCKNAME-instance number" from the selection of single point from the above mentioned block?

I am able to do it from a selected segment from the block which is named like "line00-blockname-01" in which 01 is the instance number of the block...
User avatar
AlexB
Posts: 500
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 268
x 445

Re: select a specific sketch block instance via API

Unread post by AlexB »

In looking at the GetSelectedObject6 method and the types returned by GetSelectedObjectType3, I don't believe there's an easy way to get the instance related to the selection.
User avatar
josh
Posts: 295
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 16
x 22
x 508

Re: select a specific sketch block instance via API

Unread post by josh »

I can’t figure out any way to get this info. Apparently, the 3D sketch which owns the block instances reports that it contains zero sketchpoints. The sketchpoints do not have a unique ID within the 3D sketch, but they report that they belong to the 3D sketch.

Dim swApp As SldWorks.SldWorks
Dim swSels As SldWorks.SelectionMgr
Dim swDoc As SldWorks.ModelDoc2
Dim swPt As SldWorks.SketchPoint
Dim vID As Variant

Sub main()

Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swSels = swDoc.SelectionManager

Set swPt = swSels.GetSelectedObject6(1, -1)

vID = swPt.GetID
Debug.Print "Name of sketch with point: " & swPt.GetSketch.Name & ". This sketch has " & swPt.GetSketch.GetSketchPointsCount2 & " points."
Debug.Print "Point ID: ", vID(0), vID(1)
User avatar
mp3-250
Posts: 630
Joined: Tue Sep 28, 2021 4:09 am
Answers: 20
Location: Japan
x 702
x 347

Re: select a specific sketch block instance via API

Unread post by mp3-250 »

I noticed if I select sketch segment point from the graphic window and look In "select other" list the point and the sketch point (probably a temporary ID like "point 6")and its instance are there.

If I could get that list from the API I would manipulate the name string to extract the block name and instance and select it. I did something sImilar with segments, but I was able to get the segment name at least.
with those points I see their full name in select others only...
JeromeP
Posts: 12
Joined: Wed Mar 24, 2021 12:40 pm
Answers: 0
x 1
x 2

Re: select a specific sketch block instance via API

Unread post by JeromeP »

A bit late but I think GetSelectByIdSpecification should do the trick
and works with points and segments, in or out of the 3DSketch.
Results: Point5/Block1-3 -Or- Point5/Block1-3@3DSketch1

Code: Select all

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swObj As Object
    Dim selectByString As String
    Dim objectTypeStr As String
    Dim objectTypeInt As Long
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swObj = swSelMgr.GetSelectedObject6(1, -1)
    If swObj Is Nothing Then MsgBox "Select a Point or Segment": Exit Sub
    swSelMgr.GetSelectByIdSpecification swObj, selectByString, objectTypeStr, objectTypeInt
    Debug.Print "Name of selected feature: " & selectByString
    Debug.Print "Type of object: " & objectTypeStr
    Debug.Print "Type of object as defined in swSelectType_e: " & objectTypeInt
End Sub
Post Reply