Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

API: offset sketch entites to current sketch from assy component 1

Status
Not open for further replies.

sygenics

Electrical
Jun 21, 2007
6
0
0
GB
Hi

I am trying to write a (what I thought was easy) macro to help me save some much needed time ;-) Basically I am editing a part in an assembly and have an active sketch open. I want to select a component or face in the assembly and run the macro. The macro will determine which part is selected and look for a specifically named sketch in the parts tree (fred) once it finds fred, it should offset all of the entities contained within fred into the currently active sketch.

So far I have got the first bit sorted (determining the part, looking for and finding the sketch) But i can't seem to grasp how to grab the entities within the sketch and offset them all from teh selected feature.

any help greatfully appreciated.

thanks

Rich.

code snippet.

selected item checked with
Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)

then skip through to find with

Set swFeat = swComp.FirstFeature
While Not swFeat Is Nothing
Debug.Print swFeat.Name
If swFeat.Name = "fred" Then

''GetSketchEntities stuck here

End If
Set swFeat = swFeat.GetNextFeature
 
Replies continue below

Recommended for you

sygenics,

The call you are looking for I believe is SketchManager.SketchUseEdge (I did not try using it in an assembly model, but it did work when I used it in a part model). Anyway, after you find "fred" you then use the call Feature.Select2 (or ModelDocExtension.SelectByID2) to select the sketch after which you call the SketchUseEdge.

SA
 
SolidAir

thanks for the reply. I just tried it but am getting 'object variable not set' error. I assume its because the sketch manager is not pointing to the parent of the selected sketch?

I just tried :
Set swModel = swFeat.GetOwnerFeature
Set swSketchMgr = swModel.SketchManager

but no joy. any ideas?

thanks

Rich
 
sygenics,

Since I cannot look over your code, I have no idea why you are getting the error message; it can be caused by many things. However, I did take a few minutes to explore your original question. The (very rough) code below is what I came up with. It is kind of crude (because I believe there may be a better way to do it) but it should get you going. I tested the macro on SolidWorks 2007 SP 4.0 (I assume you are running SW2005 or earlier because SelectionMgr.GetSelectedObjectsComponent2 has been obsolete since SW2006). I had an assembly open containing two parts. On part was set to edit mode and I had created a sketch in it. I clicked on a face of the other part (which contained a sketch named 'fred')to pre-select it and ran the code.

SA

Code:
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swSkMgr As SldWorks.SketchManager
Dim swFeat As SldWorks.Feature
Dim swComp As SldWorks.Component2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swAsmDoc As SldWorks.ModelDoc2

Dim retval As Boolean
Dim PartName As String
Dim AssyName As String

Sub main()

    Set swApp = Application.SldWorks
    
    Set swAsmDoc = swApp.ActiveDoc
    
    AssyName = swAsmDoc.GetTitle
    
    If InStr(1, AssyName, ".sldasm", vbTextCompare) Then
    
        AssyName = Replace(AssyName, ".sldasm", "", 1, -1, vbTextCompare)
        
    End If
    
    Set swSelMgr = swAsmDoc.SelectionManager
    
    Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)
    
    PartName = swComp.Name2
    
    Set swDoc = swComp.GetModelDoc
    
    Set swSkMgr = swAsmDoc.SketchManager

    Set swFeat = swDoc.FirstFeature
    
    Do Until swFeat Is Nothing
    
        If swFeat.Name = "fred" Then
        
            retval = swAsmDoc.Extension.SelectByID2("fred@" & PartName & "@" & AssyName, "SKETCH", 0, 0, 0, False, -1, Nothing, 0)
            
            swSkMgr.SketchUseEdge False
            
            Exit Do
            
        End If
        
        Set swFeat = swFeat.GetNextFeature
        
    Loop

End Sub
 
Hi Solidair

It works like a charm, thanks! I just need to change it now so it iterates over a whole selection of parts rather than just one :)

Do you know how to fully resolve a part so that its tree is available? (not there in lightweight)

Thanks for your help, much apreciated

Rich
 
thanks man!

I noticed that the macro doesn't work if you have selected a part in a sub assy rather than the current level assy. The API is a bit confusing here on how to select parts in a sub. Any ideas?

Rich
 
sygenics,

It certainly gets more complicated. What you have to do is parse the value returned by swComp.Name2 (again there is probably an easier way but, I do not know how without more study). For example, if the sketch "fred" was three sub-assemblies deep, swComp.Name2 would return: Assem3-1/Assem2-1/Assem1-1/Part2-1. To use it with SelectByID2 you would have to parse the value to: fred@Assem3-1@Assem4/Assem2-1@Assem3/Assem1-1@Assem2/Part2-1@Assem1. If you look close, you can see that there is a pattern. Once you figure the pattern out, you should be able to write a parsing routine. If I come up with a better way to do it, I will let you know.

SA
 
Status
Not open for further replies.
Back
Top