Hide Table of Contents

Select and Rename Objects Example (VBA)

The example shows how to verify the name of the edge and then change it. You could also select the edge programmatically using IFace2::GetEdges or ICoEdge::GetEdge. It is assumed that the user has selected an edge.

  • Verify that the user has made a selection

  • Grab the dispatch handle to the selected object

  • Determine the type of object selected

  • Determine the name of the object

  • Give the entity a name if one does not already exist

 

'------------------------------------------------------------

Sub GetAndSetEdgeName()

 

' Define variable used to hold the ISldWorks object

Dim swApp As Object 

 

' Define variable used to hold the IModelDoc and IPartDoc objects

Dim Model, Part As Object 

 

' Define variable used to hold the ISelectionManager object

Dim SelMgr As Object 

 

' Variable for the selected object

Dim selObj As Object 

Dim edgeName, messageString As String

 

' Constant enumerator

Const swSelEDGES = 1 

 

' Attach to or open SolidWorks session

Set swApp = CreateObject("SldWorks.Application")  

' Grab the current document

Set Model = swApp.ActiveDoc    

' IPartDoc and IModelDoc are same in VB

Set Part = Model    

 

' Get the ISelectionMgr class

Set SelMgr = Model.SelectionManager()  

 

' If user has selected something

If (SelMgr.GetSelectedObjectCount <> 0) Then   

' Get the first item in the selection list

Set selObj = SelMgr.GetSelectedObject5(1)   

 

' If selected object is an edge

If (SelMgr.GetSelectedObjectType(1) = swSelEDGES) Then 

' Get the name of the selected edge

edgeName = Part.GetEntityName(selObj)  

 

' If name change is successful

If (edgeName = "") Then    

' Change the name of the edge

ret = Part.SetEntityName(selObj, "NewEdgeName")  

 

' If name change is successful

If (ret = True) Then    

swApp.SendMsgToUser ("Successfully gave the edge a name")

 

' If name change failed

Else    

swApp.SendMsgToUser ("Error changing edge name.")

End If

  Else

messageString = "Edge already has name of " + edgeName

 

' Display edge name to the user

swApp.SendMsgToUser (messageString)   

  End If

 

' If selected object was not an edge

Else  

swApp.SendMsgToUser ("Please Select an Edge for this operation.")

' End if selected object was edge or not

End If    

 

' End if there is an object selected

End If   

 

End Sub

 

 



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Create and Rename Objects Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2014 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.