×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Macro to draw rectangles
2

Macro to draw rectangles

Macro to draw rectangles

(OP)
I my continuous effort to eliminate AutoCAD from daily use I need help to create a macro to do the following:

1. User selects a drawing view
2. User starts macro
3. Macro draws one rectangle around the view, offset .125" from extremities and a second rectangle offset .125" from the first rectangle. The two rectangles should be on a different layer and colored red.

The question here is how to determine xmin, ymin and xmax ymax of a selected drawing view?

RE: Macro to draw rectangles

I'm curious, why would you want to draw a box around a view? I have never seen anyone do it. Just for my info. Thanks.

Chris
Systems Analyst, I.S.
SolidWorks/PDMWorks 05
AutoCAD 06
ctopher's home site (updated 06-21-05)
FAQ559-1100
FAQ559-716

RE: Macro to draw rectangles

Is this for "Stock Size" material selection, or packaging info or ???

cheers
Helpful SW websites  FAQ559-520
How to get answers to your SW questions  FAQ559-1091

RE: Macro to draw rectangles

(OP)
He-he-he!!!

We have a laser cutting machine and a software to generate the programs for laser cuts. The software accepts dxfs as input but, to generate the programs correctly, the actual contour (drawn in white) has to be enclosed by two extra contours drawn in red. Please don't ask me why!
As of now, we export SW flat patterns to dwg, add the two boxes in AutoCAD and save as dxfs.

I found this in SW API help: View.GetOutline(). It might do the trick.

RE: Macro to draw rectangles

Why not export to DXF from a SolidWorks drawing?
I'm not a fan of creating in SW, then use it in ACAD.

Chris
Systems Analyst, I.S.
SolidWorks/PDMWorks 05
AutoCAD 06
ctopher's home site (updated 06-21-05)
FAQ559-1100
FAQ559-716

RE: Macro to draw rectangles

(OP)
That is my intention but I need to get my rectangles in somehow.

RE: Macro to draw rectangles

Seems to me the laser-cutter programmer wants the box for alignment. The program he uses accepts red because it works for him. I would create a blank dwg, insert the part and create the view, then save as DXF, then send to him. Bypass ACAD.

Chris
Systems Analyst, I.S.
SolidWorks/PDMWorks 05
AutoCAD 06
ctopher's home site (updated 06-21-05)
FAQ559-1100
FAQ559-716

RE: Macro to draw rectangles

(OP)
There's no other programmer for the laser machine. I am generating the programs with a piece of software installed on my computer. I didn't get any training to use it. I've been shown how to input the contour as DXF and save the NC program. And I've been told that every part has to have the rectangles around the contour. And they should be red because the software does recognize red as not real contours, (yellow is for etching, white for cutting, all other colours are ignored). I could bypass AutoCAD if I could find a quick way to generate those contours in SW. So far AutoCAD is much faster.

I think the View.GetOutline will not work. I may need to actually select the points in the view.That would be ok. The question is how to determine X and Y for a selected vertex in a drawing view.

RE: Macro to draw rectangles

If you're OK with selecting your four vertex points in advance then all you need to get the coordinates of those points is the GetSelectionPoint method of the SelectionManager.  The code below only operates on the last object picked.  Since you'll be picking four points, you'll need to use NumSelects, Numselects-1, Numselects-2, and NumSelects-3 to get all four points data loaded into StickPoint1 through 4.  Depending on how much more programming you want to do you can either remember to always pick the points in the same order or let the macro determine which points are maxes and mins.

CODE

Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
NumSelects = SelMgr.GetSelectedObjectCount
Set SelectedObj = SelMgr.GetSelectedObject(NumSelects)
PickPoint = SelMgr.GetSelectionPoint(NumSelects)
If SelMgr.GetSelectedObjectType2(NumSelects) > 3 Then
    StickPoint = PickPoint
Else
    StickPoint = SelectedObj.GetClosestPointOn(PickPoint(0), PickPoint(1), PickPoint(2))
End If

RE: Macro to draw rectangles

dogarila, Maybe you should get training on the machine and software. You may find an easy way to do this. Ask the manufacture for suggestions how to make this work from SolidWorks.

Chris
Systems Analyst, I.S.
SolidWorks/PDMWorks 05
AutoCAD 06
ctopher's home site (updated 06-21-05)
FAQ559-1100
FAQ559-716

RE: Macro to draw rectangles

Quote:

I am generating the programs with a piece of software installed on my computer
I realize that CAM software is generally not cheap, but if you have to do this often, your company could very quickly start to save money by upgrading to one which is able to use solid geometry directly from SW.
What is your current program called?

cheers
Helpful SW websites  FAQ559-520
How to get answers to your SW questions  FAQ559-1091

RE: Macro to draw rectangles

(OP)
CBL, the software is called TOPS100 version 4.0 and I think it's the latest for our machine.

handleman, does the code work for drawing views?

RE: Macro to draw rectangles

Why not put the rectangles in your part template or make a laser part template?  Then maybe make a formula so it always stay .125 from the extremities.  Or just go back and edit that dim.  Just a thought off the top of my head.  Might not work.

Grant
Applications Engineer
SW2006 SP 3.4
IBM InteliStation Pro M
P4 3.4 GHz, 2GB RAM
XP Pro SP2.0
NIVIDA Quadro FX 1100

RE: Macro to draw rectangles

Sorry, I probably should have elaborated a bit more.  What's there is a snippet out of a macro I wrote to insert surface finish symbols back before surface finishes were integrated into a property manager (2005, I think).  We have just a handful of symbols we use every day, so the large extra dialog box was cumbersome for me to use.  I don't think the code will work exactly as-is, since it's cut out of the middle of a longer function.  You'll have to do a little bit of tinkering around, but that is the general idea of what you'll need to do.  

Long story short, these functions work in drawings.

RE: Macro to draw rectangles

This isn't a one-step process, but on the Kansas City Users Group site there is a free macro called BoundingBox.swp.  http://kcswug.com/programs/macros/

It will draw a minimum bounding box on your part.  After using it, on your drawing, you would have to show the sketch, then offset it 1/8" twice.  If you already have ACAD installed on your computer, I would use it instead.  You might even find an AutoLISP program written to do what you want.  

Flores
SW06 SP3.0

RE: Macro to draw rectangles

2
I think this is a reasonable request.  This is what the API is for - to automate routine everyday tasks.

The following code will do it.  The offset value is in meters (SW API works in metric units) and is given as a constant that you can change if required.  You will need to create your red layer in the drawing template and have that layer active when you run the macro.  

Two lines are commented out - they have the ' at the  beginning of the line.  If you uncomment these lines you will get the rectangles around all the drawing views.  Having the comments in there stops the loop at the first drawing view.

If this was my routine, I would also put the dxf export in the code so you don't have to do it manually.  It should not be difficult for you to add that in.

CODE

Option Explicit
Const OFFSET = 0.00317

Sub Main()

    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swDraw                      As SldWorks.DrawingDoc
    Dim swView                      As SldWorks.View
    Dim vOutline                    As Variant
    Dim vPos                        As Variant
    Dim i                           As Long
    Dim lViewNumber                 As Long
 
    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swView = swDraw.GetFirstView
    Set swView = swView.GetNextView

    If swView Is Nothing Then
        End
    Else
        lViewNumber = 1
        swModel.SetAddToDB (True)
    End If


'    Do While Not swView Is Nothing

        vOutline = swView.GetOutline
        vPos = swView.Position

        Debug.Print "View = " + swView.Name
        Debug.Print "  Pos = (" & vPos(0) * 1000# & ", " & vPos(1) * 1000# & ") mm"
        Debug.Print "  Min = (" & vOutline(0) * 1000# & ", " & vOutline(1) * 1000# & ") mm"
        Debug.Print "  Max = (" & vOutline(2) * 1000# & ", " & vOutline(3) * 1000# & ") mm"
        Debug.Print

        Set swView = swDraw.GetFirstView
        swModel.SketchRectangle vOutline(0) - OFFSET, vOutline(3) + OFFSET, 0, _
                vOutline(2) + OFFSET, vOutline(1) - OFFSET, 0, True
        swModel.SketchRectangle vOutline(0) - 2 * OFFSET, vOutline(3) + 2 * OFFSET, 0, _
                vOutline(2) + 2 * OFFSET, vOutline(1) - 2 * OFFSET, 0, True

        lViewNumber = lViewNumber + 1
        For i = 1 To lViewNumber
            Set swView = swView.GetNextView
        Next i

'    Loop
    
    With swModel
        .ClearSelection2 (True)
        .SetAddToDB (False)
        .EditRebuild3
    End With
    
End Sub
   

RE: Macro to draw rectangles

(OP)
Stoker, thank you for your support. Your code looks promising. I copied and pasted it into a new macro and what it does it draws two small rectangles in the lower left corner of the format. I have a title block with a view. If I have the view selected it draws two small rectangles in the upper right corner of the view.

RE: Macro to draw rectangles

(OP)
I changed the scale of my view to 1:1 and now it draws the two rectangles correctly. How do I alter the code to take the scale in consideration?

RE: Macro to draw rectangles

You don't need to preselect the view.  If nothing is selected it works fine on my machine.  Are the rectangles still not positioned properly when nothing is selected prior to running the routine?

RE: Macro to draw rectangles

You are creating dxf's of flat patterns that aren't at 1:1 scale?

RE: Macro to draw rectangles

If I understand your posts correctly, you want these rectangles to be 0.125" from the extremeties of the part itself, correct?  These boxes will be 0.125" from the bounding box around the view, which is some distance away from the actual model edges.

RE: Macro to draw rectangles

(OP)
I added this line:

swDraw.ActivateView(swView.GetName2)

to activate the current view and translated the coordinates to draw the rectangles in the current view. It works fine when scale is 1:1. At scale 1:4 the rectangles are in the right position but they are 4 times smaller.

The dxf's will be 1:1 but in SW I have to fit them in the title block. In SW you can't scale the titleblock. I am sure there must be a way to draw the rectangles considering the scale effect.

One remark. The outline is not exactly the min and max of my view. In my test the outline was already 0.34" offset from the contour so in the end I may not use it and try again by selecting points.

RE: Macro to draw rectangles

(OP)
Exactly, handleman.

RE: Macro to draw rectangles

Yes the original routine will not work properly if the scale is not 1:1.  Although I'm not sure why you would not be creating flat patterns to scale, here is what the routine looks like with the view scale factor added in.

Yes the rectangles are offset from the drawing view border, which is itself offset from the actual edges of the part.  You can adjust the offset value as you require to get the offset you want from part edges to sketched rectangles.  Calculating the actual position of the part edges is a more involved process than is likely required.

Do not select the view prior to running.

CODE

Option Explicit
Const OFFSET = 0.00317

Sub Main()

    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swDraw                      As SldWorks.DrawingDoc
    Dim swView                      As SldWorks.View
    Dim vOutline                    As Variant
    Dim vPos                        As Variant
    Dim i                           As Long
    Dim lViewNumber                 As Long
 
    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swView = swDraw.GetFirstView
    Set swView = swView.GetNextView

    If swView Is Nothing Then
        End
    Else
        swModel.EditRebuild3
        lViewNumber = 1
        swModel.SetAddToDB (True)
    End If


'    Do While Not swView Is Nothing

        vOutline = swView.GetOutline
        vPos = swView.Position
        
        Debug.Print "View = " + swView.Name
        Debug.Print "  Pos = (" & vPos(0) * 1000# & ", " & vPos(1) * 1000# & ") mm"
        Debug.Print "  Min = (" & vOutline(0) * 1000# & ", " & vOutline(1) * 1000# & ") mm"
        Debug.Print "  Max = (" & vOutline(2) * 1000# & ", " & vOutline(3) * 1000# & ") mm"
        Debug.Print

        Set swView = swDraw.GetFirstView
        swModel.SketchRectangle (vOutline(0) - OFFSET) / swView.ScaleDecimal, _
                                (vOutline(3) + OFFSET) / swView.ScaleDecimal, 0, _
                                (vOutline(2) + OFFSET) / swView.ScaleDecimal, _
                                (vOutline(1) - OFFSET) / swView.ScaleDecimal, 0, True
        
        swModel.SketchRectangle (vOutline(0) - 2 * OFFSET) / swView.ScaleDecimal, _
                                (vOutline(3) + 2 * OFFSET) / swView.ScaleDecimal, 0, _
                                (vOutline(2) + 2 * OFFSET) / swView.ScaleDecimal, _
                                (vOutline(1) - 2 * OFFSET) / swView.ScaleDecimal, 0, True

        lViewNumber = lViewNumber + 1
        For i = 1 To lViewNumber
            Set swView = swView.GetNextView
        Next i

'    Loop
    
    With swModel
        .ClearSelection2 (True)
        .SetAddToDB (False)
        .EditRebuild3
    End With
    
End Sub

RE: Macro to draw rectangles

Also notice that if you make the value of the "OFFSET" constant negative, it will offset the rectangles closer to the model, while a positive value will offset away from the mode.

RE: Macro to draw rectangles

(OP)
Thank you Stoker. Now I have a working macro. I still don't like the offset of the outline from part edges and I will look in selecting points again. Anyway I see the light at the end of the tunnel.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources