VBA - Copy sketch from another part file

Is it possible to copy a sketch(with dims & relations) from a part(not open) to an active part via VBA....i know it can be done manually with both open.  But would like to copy some sketches from some master files, into working files.

SolidworksApi macros
Answers 9
PB
PB 2017-07-20 Best Answer

I suggest you open the master file in code. If you really don't want the user to notice, you could hide the opened file.

CA
CA 2017-07-21

Ok, how would i go about it.  Ive never copied any feature from one file to another via VBA.

AJ
AJ 2017-07-21

​: Can you not record a macro and paste it here? If you can play the macro back then it can be done with some commands. Perhaps EditCopy EditPaste. I'm not sure though. It can certainly be done by redrawing the entities but that's an overfetched solution.

JS
JS 2017-07-21

Don't know if this would help or not

CA
CA 2017-07-21

Amen,

I can cut and paste, but that requires that both file be open and active, and changing windows. I would like to make the master(paste from) file hidden or not open.

What got recorded:

Dim swApp As Object

Dim Part As Object

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

Sub main()

   

    Set swApp = Application.SldWorks

   

    Set Part = swApp.ActiveDoc

    boolstatus = Part.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

    Part.EditCopy

    Set Part = swApp.ActiveDoc

    Dim myModelView As Object

    Set myModelView = Part.ActiveView

    myModelView.FrameLeft = 0

    myModelView.FrameTop = 22

    Set myModelView = Part.ActiveView

    myModelView.FrameState = swWindowState_e.swWindowMaximized

    swApp.ActivateDoc2 "test.SLDPRT", False, longstatus

    Set Part = swApp.ActiveDoc

    Part.Paste

End Sub

AJ
AJ 2017-07-21

​ : That could be a starting point but recreating an entire sketch is reallly reallly painful. You have:
- SketchPoint
- SketchLines
- SketchARCs (circle and arcs alike)
- SketchEllipses
- SketchSplines
- Constraints
- Dimensions (potentially driven by a custom property which makes it all annoying)
- SketchBlocks

and more importantly the original sketch may have a different base plane then the target sketch.

It seems that saving the first sketch as a dwg and importing it to the new sketch is much easier but that may take a bit of time (4-6 seconds).

JS
JS 2017-07-21

​ - I know nothing about API except to maybe search for different topics and I can do a slight edit

AJ
AJ 2017-07-21

Hidden has nothing to do with a file being open and active.

Try set the ModelDoc2::Visible to false and see if it works.

AJ
AJ 2017-07-21

​ : Sometimes those edits make you the expert!