API - how to unhide all sketches of an assembly in drawing views

Discussion in 'SolidWorks' started by Ronan, Oct 20, 2005.

  1. Ronan

    Ronan Guest

    I try to access all sketches of an assembly in a drawing to then unhide
    them to enable the "Insert model items" to automatically import the
    sketches dimensions in the drawings.
    My first step is to test the way to access those sketches but I can't.
    I'm stopped at the RootDrawingComponent level. I don't manage to go
    further. Impossible to edit the first feature.
    Here one of my test codes to explain where I would like to go. Any
    suggestion?
    Thanks,
    Ronan
    Solidworks 2005 SP5



    Sub ParFonction()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSheet As SldWorks.Sheet
    Dim swView As SldWorks.View
    Dim swRootDrawComp As SldWorks.DrawingComponent
    Dim swComp As SldWorks.Component2
    Dim swFeat As SldWorks.Feature

    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    ' Make sure that the active document is a drawing
    'If swModel.GetType <> swDocDRAWING Then
    If swModel.GetType <> 3 Then
    Msg = "Only Allowed on Drawings" ' Define message
    Style = vbOKOnly ' OK Button only
    Title = "Error" ' Define title
    Call MsgBox(Msg, Style, Title) ' Display error message
    Exit Sub ' Exit this program
    End If

    Set swSheet = swModel.GetCurrentSheet
    Set swView = swModel.GetFirstView
    Debug.Print " "
    Debug.Print "***********************"
    Debug.Print "Doc =" & swModel.GetPathName
    Debug.Print "Sheet =" & swSheet.GetName

    While Not swView Is Nothing
    Debug.Print " "
    Debug.Print "View = " & swView.GetName2 & " [" & swView.Type & "]"
    Debug.Print "View = " & swView.Name
    Debug.Print "Ref = " & swView.GetReferencedModelName

    Set swRootDrawComp = swView.RootDrawingComponent
    If Not swRootDrawComp Is Nothing Then
    Debug.Print "RtComp = " & swRootDrawComp.Name
    Set swComp = swRootDrawComp.Component
    Set swFeat = swComp.FirstFeature
    Do While Not swFeat Is Nothing
    Debug.Print swFeat.Name
    Set swFeat = swFeat.GetNextFeature
    Loop
    End If
    Set swView = swView.GetNextView
    Wend

    End Sub
     
    Ronan, Oct 20, 2005
    #1
  2. Ronan

    dudi Guest

    Hi Ronan
    in order to hide or show model sketces in view
    you have to select them using their full view base name
    i.e.
    boolstatus = drwPart.Extension.SelectByID2("Sketch1@base
    plate-4@Drawing View1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
    and then use
    drwPart.BlankSketch

    You can travers the drawingcomponent tree in order to get to the sketch
    name.
    If the rootcomponent is an assembly you can get its children
    then the component of each childern and their features names and select
    them (using full view based path name)

    if the drawing rootcomponent is not an assembly however you will get
    nothing when you try to reach the component of the rootcomponent to
    walk through its features . in this case get the referenced doc and get
    the sketches names and select them using full view based path name

    regards
    dudi peer
     
    dudi, Oct 22, 2005
    #2
  3. Ronan

    Ronan Guest

    Then, if I understand, you could only traverse children of an assembly
    through the DrawingComponent not the referenced component in the view
    itself.
    The only way is the view based path name to change sketches state like
    recorded macro does. But as I want to change the state of any of them of
    any referenced document, I don't know the name of the sketches.
    Traversing features would be the solution but it's not possible. There
    shouldn't be a solution for my probleme. Is that right or is there other
    ways to do?
    Regards
    Ronan
     
    Ronan, Oct 23, 2005
    #3
  4. Ronan

    dudi Guest

    Hi
    You can traverse the assembly via the drawingcomponent.
    once you have the drawing component of an assembly component you can
    get the component (drawingcomponent.component) and travers its
    features. then you can use the featurec typename and if it is a profile
    you can get its name (feature.name) and then you can built the name
    sketcname@componentname@viewname and use selectbyid once selected this
    way. you can hide or unhide it/ its combersome but its the only way i
    have succeded . maybe there is aother way but i havent found it. i have
    try to use select2 while i get the feature but its not actualy selected
    in the view so it would not hide.
    dudi peer
    eci telecom
     
    dudi, Oct 24, 2005
    #4
  5. Ronan

    dudi Guest

    HI Ronan
    I wrote the program and i am posting it here
    since found that a little tricky
    put the content in a nacro and you can run it

    '================================================
    Dim swApp As SldWorks.SldWorks
    Dim oModeldoc As SldWorks.ModelDoc2
    Dim oDrwDoc As DrawingDoc
    Dim oView As SldWorks.View
    Dim bStatus As Boolean
    Dim oDrwComp As DrawingComponent
    Dim oSwSheet As SldWorks.Sheet
    Dim m_StrViewName As String


    Sub main()
    Dim i As Integer
    Dim oRootCompDoc As ModelDoc2
    Dim strConfigName As String
    Dim oConfig As Configuration
    Dim strFullSketcName As String
    Dim lerr As Long
    Dim vChildren As Variant
    Dim oFeature As SldWorks.Feature
    Dim oComp As Component2


    Set swApp = Application.SldWorks
    Set oModeldoc = swApp.ActiveDoc ' getting the active doc
    'testing for drawing
    If oModeldoc.GetType <> swDocDRAWING Then
    swApp.SendMsgToUser ("Document is not drawing")
    Exit Sub
    End If

    Set oDrwDoc = oModeldoc ' just for convinence to enable autocomplete
    Set oSwSheet = oDrwDoc.GetCurrentSheet 'getting the current sheet
    If oSwSheet Is Nothing Then
    swApp.SendMsgToUser ("cannot get current sheet")
    Exit Sub
    End If

    Set oView = oDrwDoc.GetFirstView 'first view is the drwing sheet sketch
    so we have to move get the next for real view
    Set oView = oView.GetNextView ' getting the first view
    Do While Not oView Is Nothing ' make sure you get a view
    m_StrViewName = oView.Name ' getting the view name
    Debug.Print "View Name: " & m_StrViewName ' for debuging
    Set oDrwComp = oView.RootDrawingComponent ' getting the root
    drawing component
    Set oRootCompDoc = oView.ReferencedDocument ' getting the drawing
    component document since the
    ' root component is not
    exactly an assembly component
    ' there for when we get
    its component we cannot get access to the first feature
    ' we use as workaround
    by going to the specific configuration of the model and getting the
    ' the sketches names
    from there
    strConfigName = oView.ReferencedConfiguration ' the current
    configuration of the root component
    Set oRootCompDoc = swApp.ActivateDoc2(oRootCompDoc.GetPathName,
    True, lerr) ' activating the reference document

    bStatus = oRootCompDoc.ShowConfiguration2(strConfigName)
    'activating the configuration
    Set oFeature = oRootCompDoc.FirstFeature ' getting the first
    fieatur of the document
    Do While Not oFeature Is Nothing ' steping through the
    feature tree of root doc
    If oFeature.GetTypeName = "ProfileFeature" Then '
    checking for sketch
    strFullSketcName = getSketchFullName(oView.Name,
    oDrwComp.Name, oFeature.Name) ' getting the sketch name for selectbyis
    bStatus =
    oModeldoc.Extension.SelectByID2(strFullSketcName, "SKETCH", 0, 0, 0,
    False, 0, Nothing, 0) ' selecting the sketch in drawing
    If bStatus Then oModeldoc.BlankSketch ' blanking the
    sketch
    End If
    Set oFeature = oFeature.GetNextFeature
    Loop
    Debug.Print "Drawing RootComp Name " & oDrwComp.Name
    If oDrwComp.GetChildrenCount > 0 Then Call
    traverse_drwcomponent(oDrwComp) ' travesing the children components
    Set oView = oView.GetNextView
    Loop

    End Sub


    Function getSketchFullName(strViewName As String, strComponentName As
    String, strSketchName As String) As String
    'this function construct the name of the sketch for selection in
    drawing
    Dim vStr As Variant
    Dim i As Long
    Dim tmpStr As String
    Dim StrParentName As String

    vStr = Split(strComponentName, "/")
    tmpStr = strSketchName & "@" & vStr(0) & "@" & strViewName
    For i = 0 To UBound(vStr) - 1
    StrParentName = Left(vStr(i), InStrRev(vStr(i), "-") - 1)
    tmpStr = tmpStr & "/" & vStr(i + 1) & "@" & StrParentName
    Next i
    getSketchFullName = tmpStr
    Debug.Print getSketchFullName
    End Function

    Sub traverse_drwcomponent(oDrcmp As DrawingComponent)
    Dim vChildren As Variant
    Dim oComp As Component2
    Dim tmpDrcmp As DrawingComponent
    Dim i As Long
    Call traverse_Features(oDrcmp)
    If oDrcmp.GetChildrenCount > 0 Then
    vChildren = oDrcmp.GetChildren
    For i = 0 To oDrcmp.GetChildrenCount - 1
    Debug.Print vChildren(i).Name
    Set tmpDrcmp = vChildren(i)
    Call traverse_drwcomponent(tmpDrcmp)
    Next i
    End If
    End Sub

    Sub traverse_Features(oDrcmp As DrawingComponent)
    'this program traverse feature of child component and blank the sketch
    in the drawing view
    Dim i As Long
    Dim oFeature As Feature
    Dim oComp As Component2
    Dim strSketchFullName As String

    Set oComp = oDrcmp.Component
    Set oFeature = oComp.FirstFeature
    Do While Not oFeature Is Nothing
    If oFeature.GetTypeName = "ProfileFeature" Then
    strSketchFullName = getSketchFullName(oView.Name, oDrcmp.Name,
    oFeature.Name)
    bStatus = oModeldoc.Extension.SelectByID2(strSketchFullName,
    "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
    If bStatus Then oModeldoc.BlankSketch
    End If
    Set oFeature = oFeature.GetNextFeature
    Loop
    End Sub
    '====================================================================
    regards
    dudi peer
     
    dudi, Oct 25, 2005
    #5
  6. Ronan

    Ronan Guest

    You can traverse the assembly via the drawingcomponent.

    Yes that is what I manage to do, going through the feature of the
    subcomponents of an assembly.
    But what I would like to list are the sketches (then "features") at the
    assembly level. That ones in the assembly document not those in the
    parts of the assembly. How to get the sketches name at the assembly
    level? I have no clue (Set swComp = swRootDrawComp.Component gives me
    nothing).
    Did you managed that?
    Thanks for your contribution.
    Ronan

    What works but only for parts of an assembly:

    While Not swView Is Nothing
    Debug.Print " "
    Debug.Print "View = " & swView.GetName2 & " [" & swView.Type & "]"
    Debug.Print "View = " & swView.Name
    Debug.Print "Ref = " & swView.GetReferencedModelName

    Set swRootDrawComp = swView.RootDrawingComponent
    If Not swRootDrawComp Is Nothing Then
    Debug.Print "RtComp = " & swRootDrawComp.Name
    vDrawChildCompArr = swRootDrawComp.GetChildren
    For Each vDrawChildComp In vDrawChildCompArr
    Set swDrawComp = vDrawChildComp
    Set swComp = swDrawComp.Component
    Debug.Print "Comp = " & swComp.Name
    Set swFeat = swComp.FirstFeature
    Do While Not swFeat Is Nothing
    Debug.Print " Feat = " & swFeat.Name
    Set swFeat = swFeat.GetNextFeature
    Loop
    Next
    End If
    Set swView = swView.GetNextView
    Wend
     
    Ronan, Oct 25, 2005
    #6
  7. Ronan

    Ronan Guest

    HI Ronan
    Sorry, I just posted a message without having seen this one. My ISP is
    good but not for his news server. I have to check both my news client
    and google to get answers.
    I will read what you posted and then answer you.
    Thanks,
    Ronan
     
    Ronan, Oct 25, 2005
    #7
  8. Ronan

    Ronan Guest

    HI Ronan
    Thanks very much for your job. That is exactly what I would like to do
    about accessing sketches in views. That saves me times and errors on
    quite some of my assemblies.
    Now I'm like an idiot because I should noticed first two thing that not
    works:

    swAnnotations = oDrwDoc.InsertModelAnnotations3(0,
    swInsertDimensionsMarkedForDrawing, True, False, True, False)
    Doesn't work in a macro for unhidden sketches, nothing happens.

    No way to automatically show dimensions after hiding sketches. It seems
    there is no function in the API to do that. Recording a macro gives no
    information.

    I will investigate but without hope. I'm a little disapointed with this
    API. It seems there is quite some holes in this tool. Workarounds aren't
    always enough.

    Regards,
    Ronan
     
    Ronan, Oct 27, 2005
    #8
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.