Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

CATIA V5 Macro - Line From Selected Point To Selected Point 2

Status
Not open for further replies.

evereux

Aerospace
Feb 17, 2013
14
Hi,

I'm trying to get my head around CATScript and thought I'd start by writing something very simple so I could get my head around some basic concepts. Mainly, selecting items and using those to create geometry. The task is to select two coordinate points and then draw a line between them. But, the script is failing at the line in red below.

Code:
Language="VBSCRIPT"

Sub CATMain()

	Dim myDocument
	Set myDocument = CATIA.ActiveDocument
	
	Dim oSelElement1 As SelectedElement
	Dim i As Integer
	Dim parameters1 As Parameters
	Dim mySelection As Selection
	Dim ThePart As Part
	Dim hybridShapeFactory1 As Factory
	Dim hybridShapePointCoord1 As Parameter
	Dim hybridShapePointCoord2 As Parameter
	Dim reference1 As Reference
	Dim reference2 As Reference
	Dim hybridShapeLinePtPt1 As hybridShapeLinePtPt
	Dim hybridBodies1 As HybridBodies
	Dim Select1 As SelectedElement
	Dim Select2 As SelectedElement
	
	Set mySelection = myDocument.Selection
	Set ThePart = myDocument.Part
	Set hybridShapeFactory1 = ThePart.HybridShapeFactory
	Set parameters1 = ThePart.Parameters
	
	Set SelectedElement1 = mySelection.Item(1)
	Set Point1 = SelectedElement1.Value
	Set SelectedElement2 = mySelection.Item(2)
	Set Point2 = SelectedElement2.Value
	
	[COLOR=#CC0000]Set hybridShapePointCoord1 = parameters1.Item(Point1.name)[/color]
	Set hybridShapePointCoord2 = parameters1.Item(Point2.name)
	Set reference1 = ThePart.CreateReferenceFromObject(hybridShapePointCoord1)
	Set reference2 = ThePart.CreateReferenceFromObject(hybridShapePointCoord2)
	Set hybridShapeLinePtPt1 = hybridShapeFactory1.AddNewLinePtPt(reference1, reference2)
	Set hybridBodies1 = ThePart.HybridBodies
	Set hybridBody1 = hybridBodies1.Item("ConstructionGeometry")

	hybridBody1.AppendHybridShape hybridShapeLinePtPt1
	ThePart.InWorkObject = hybridShapeLinePtPt1

	ThePart.Update

End Sub

If anyone could help point me in the right direction it would be very much appreciated.

Thanks
 
Replies continue below

Recommended for you

Cracked it.Simply
Code:
	Set hybridShapePointCoord1 = parameters1.Item(Point1.name)
Set hybridShapePointCoord2 = parameters1.Item(Point2.name)
Should have been
Code:
	Set hybridShapePointCoord1 = Point1
Set hybridShapePointCoord2 = Point2

Therefore it could be simplified further by saying
Code:
Set hybridShapePointCoord1 = SelectedElement1.Value
foreach point instead of doing it in two lines as I had done.
 
Hi,

Or you can ask user to select points one by one. Line will be created in first existing GS, doesn't matter the name (look also in lower left corner to see how InputObjectType is working).

Code:
Sub CATMain()

Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument

Dim part1 As Part
Set part1 = partDocument1.Part

Dim hybridShapeFactory1 As Factory
Set hybridShapeFactory1 = part1.HybridShapeFactory

Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = part1.HybridBodies

Dim hybridBody1 As HybridBody
Set hybridBody1 = hybridBodies1.Item(1)

Dim hybridShapes1 As HybridShapes
Set hybridShapes1 = hybridBody1.HybridShapes

Dim selection1 As Selection
Set selection1 = partDocument1.Selection
selection1.Clear

Dim InputObjectType(0), Status1
InputObjectType(0)="Point"
Status1=selection1.SelectElement2(InputObjectType,"Select 1st Point",false)
If Status1 = "Cancel" Then selection1.Clear: Exit Sub

Dim reference1 As Reference
Set reference1 = selection1.Item(1).Reference

MsgBox reference1.Name

selection1.Clear
Status1=selection1.SelectElement2(InputObjectType,"Select 2nd Point",false)
If Status1 = "Cancel" Then selection1.Clear: Exit Sub

Dim reference2 As Reference
Set reference2 = selection1.Item(1).Reference

MsgBox reference2.Name

Dim hybridShapeLinePtPt1 As HybridShapeLinePtPt
Set hybridShapeLinePtPt1 = hybridShapeFactory1.AddNewLinePtPt(reference1, reference2)

hybridBody1.AppendHybridShape hybridShapeLinePtPt1

part1.InWorkObject = hybridShapeLinePtPt1

part1.Update 


End Sub

Regards
Fernando

 
That's a nice tip!

I found SelectElement2 in the V5 automation handbook which if anyone is interested says this:
[tt]SelectElement2
Runs an interactive selection command.
Role: SelectElement2 asks the end user to select a feature (in the geometry or in the specification tree) in a Window of the active Document . During the selection, when the end user will move the mouse above a feature which maps the given filter, the mouse pointer will be the "hand" cursor, and when end user will move the mouse above a feature which does not map the given filter, the mouse pointer will be the "no entry" cursor.[/tt]

It's slowly, but surely, coming together ....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor